Working with DateTime4:21 with Phil Sturgeon
Dates and times in PHP used to be quite a mess in the PHP 4 days, but the DateTime class has brought much easier workflows.
PHP has a code class called Date Time. 0:00 This will help you in reading, writing, comparing dates, or 0:02 making date time rated calculations. 0:05 There are many date and 0:07 time rated functions in PHP besides the date time class, but 0:08 this provides a nice object oriented interface to most of the common uses. 0:12 So let’s start off by opening index for PHP. 0:16 On line three, we’re going to create a new date time object and 0:18 store it in the date variable. 0:21 In this case, 0:23 we'll be creating a object by passing a string into the class constructor. 0:24 This string will contain a date, which can be in one of a bunch of different formats. 0:28 We'll look at some of these formats shortly, but for 0:32 now, this example has an input format of year, comma, August 23. 0:33 On line seven we're outputting a paragraph of the text. 0:38 The output day is, then using the format method on 0:41 our date time object output the date in our own specific format. 0:44 Our example here use M slash D slash capital Y is an output format. 0:48 Meaning when we run this script we should expect to see 08/23/2014. 0:53 Let's take a look in preview mode, by clicking on the eye icon. 0:59 Cool, that works. 1:01 Here we have the output date is 08/23/2014, just like we expected. 1:07 The benefit here is that we can change the input date string, and 1:10 have the same output format. 1:13 One alternative commonly used in put format, is the SQL date string. 1:15 Let's go back to the work space. 1:19 Back to index.php and let's enter a new date and a new format. 1:21 So I'm gonna keep the year 2014. 1:27 I'm gonna change things around a little bit and enter 09-12. 1:29 Which is the 12th of September. 1:33 I save that out. 1:37 Then refresh. 1:39 Great, we have the exact same date format, but it has a different date. 1:40 If we go out back to the workspace, we can get a little bit more adventurous with 1:46 initializing date time objects using string offsets. 1:49 We can change the constructor to take a string starting with a plus or a minus. 1:52 Then we add number of units and 1:57 define the unit with the words second, hours, days, weeks, months, or even years. 1:58 So if we enter plus two weeks and have a little look, see what happens there. 2:04 Then we will have an output which is two weeks from now. 2:13 Brilliant. 2:17 You can jump forwards using more human-friendly phrases, too. 2:21 We can change this string to contain phrases like next week or tomorrow. 2:24 So let's have a go at that. 2:28 Next week. 2:30 Perfect. 2:33 Tomorrow. 2:35 There we go. 2:40 As I mentioned earlier the string argument passed to the date time constructor on 2:42 line three can accept dates in many different input formats. 2:45 To learn more about the date time formats we can use the PHP manual. 2:48 There is a link in the notes for 2:52 this video called PHP date formats so let's head over there. 2:53 [SOUND] You scroll about half way down. 2:56 You can see under localized notations there's a whole bunch of 3:01 different formats supported. 3:05 Some American ones. 3:07 Some more generic. 3:08 The format here. 3:10 This is a literal string. 3:12 So that means it's actually a slash, and not some special meaning. 3:13 And then you can see some actual examples of how they look. 3:16 So, when you use them they'll come out without the quotation marks around them. 3:19 And there's some other fairly common formats here. 3:24 This one, like the four digit year, month, and 3:27 day with slashes could be one that you might want to use. 3:29 Sometimes you will need to work with dates that are stored in unsupported formats, 3:32 for example if a string contains two date segments with numeric values below 13 they 3:36 could both be days or months. 3:41 Luckily the date time class has a method called create from format we can 3:43 provide our custom input format to this method along with our date so 3:47 that PHP can read the string correctly. 3:50 If we go back to our workspace, paste this code in. 3:52 On line three here we have a string containing an unsupported date format. 3:57 We store it in the raw variable and pass it to the create from format method. 4:01 If all goes well, 4:05 when we run this once again, we should see our date output in the same format. 4:06 This time the date should be 11/10/1968. 4:10 Save that and see if it works. 4:16 Perfect! 4:20 Let's move on. 4:20
You need to sign up for Treehouse in order to download course files.Sign up