1 00:00:00,370 --> 00:00:02,850 PHP has a code class called Date Time. 2 00:00:02,850 --> 00:00:05,530 This will help you in reading, writing, comparing dates, or 3 00:00:05,530 --> 00:00:07,990 making date time related calculations. 4 00:00:07,990 --> 00:00:08,740 There are many date and 5 00:00:08,740 --> 00:00:12,010 time related functions in PHP besides the date time class, but 6 00:00:12,010 --> 00:00:15,430 this provides a nice object oriented interface to most of the common uses. 7 00:00:16,550 --> 00:00:18,962 So let’s start off by opening index dot PHP. 8 00:00:18,962 --> 00:00:21,940 On line three, we’re going to create a new date time object and 9 00:00:21,940 --> 00:00:23,990 store it in the date variable. 10 00:00:23,990 --> 00:00:24,870 In this case, 11 00:00:24,870 --> 00:00:28,360 we'll be creating a object by passing a string into the class constructor. 12 00:00:28,360 --> 00:00:32,190 This string will contain a date, which can be in one of a bunch of different formats. 13 00:00:32,190 --> 00:00:33,860 We'll look at some of these formats shortly, but for 14 00:00:33,860 --> 00:00:38,360 now, this example has an input format of year, comma, August 23. 15 00:00:38,360 --> 00:00:41,750 On line seven we're outputting a paragraph of the text. 16 00:00:41,750 --> 00:00:44,470 The output day is, then using the format method on 17 00:00:44,470 --> 00:00:48,150 our date time object output the date in our own specific format. 18 00:00:48,150 --> 00:00:53,550 Our example here use M slash D slash capital Y is an output format. 19 00:00:53,550 --> 00:00:59,140 Meaning when we run this script we should expect to see 08/23/2014. 20 00:00:59,140 --> 00:01:01,358 Let's take a look in preview mode, by clicking on the eye icon. 21 00:01:01,358 --> 00:01:07,020 Cool, that works. 22 00:01:07,020 --> 00:01:10,630 Here we have the output date is 08/23/2014, just like we expected. 23 00:01:10,630 --> 00:01:13,480 The benefit here is that we can change the input date string, and 24 00:01:13,480 --> 00:01:15,420 have the same output format. 25 00:01:15,420 --> 00:01:19,830 One alternative commonly used input format, is the SQL date string. 26 00:01:19,830 --> 00:01:21,678 Let's go back to the work space. 27 00:01:21,678 --> 00:01:26,180 Back to index.php and let's enter a new date and a new format. 28 00:01:27,450 --> 00:01:29,000 So I'm gonna keep the year 2014. 29 00:01:29,000 --> 00:01:33,500 I'm gonna change things around a little bit and enter 09-12. 30 00:01:33,500 --> 00:01:35,380 Which is the 12th of September. 31 00:01:37,030 --> 00:01:37,750 I save that out. 32 00:01:39,660 --> 00:01:40,970 Then refresh. 33 00:01:40,970 --> 00:01:44,500 Great, we have the exact same date format, but it has a different date. 34 00:01:46,190 --> 00:01:49,320 If we go out back to the workspace, we can get a little bit more adventurous with 35 00:01:49,320 --> 00:01:52,490 initializing date time objects using string offsets. 36 00:01:52,490 --> 00:01:57,100 We can change the constructor to take a string starting with a plus or a minus. 37 00:01:57,100 --> 00:01:58,700 Then we add number of units and 38 00:01:58,700 --> 00:02:04,830 define the unit with the words second, hours, days, weeks, months, or even years. 39 00:02:04,830 --> 00:02:11,950 So if we enter plus two weeks and have a little look, see what happens there. 40 00:02:13,280 --> 00:02:17,030 Then we will have an output which is two weeks from now. 41 00:02:17,030 --> 00:02:17,530 Brilliant. 42 00:02:21,790 --> 00:02:24,860 You can jump forwards using more human-friendly phrases, too. 43 00:02:24,860 --> 00:02:28,960 We can change this string to contain phrases like next week or tomorrow. 44 00:02:28,960 --> 00:02:30,290 So let's have a go at that. 45 00:02:30,290 --> 00:02:30,790 Next week. 46 00:02:33,180 --> 00:02:33,680 Perfect. 47 00:02:35,580 --> 00:02:36,180 Tomorrow. 48 00:02:40,670 --> 00:02:42,000 There we go. 49 00:02:42,000 --> 00:02:45,400 As I mentioned earlier the string argument passed to the date time constructor on 50 00:02:45,400 --> 00:02:48,880 line three can accept dates in many different input formats. 51 00:02:48,880 --> 00:02:52,730 To learn more about the date time formats we can use the PHP manual. 52 00:02:52,730 --> 00:02:53,770 There is a link in the notes for 53 00:02:53,770 --> 00:02:56,895 this video called PHP date formats so let's head over there. 54 00:02:56,895 --> 00:03:01,774 [SOUND] You scroll about half way down. 55 00:03:01,774 --> 00:03:05,464 You can see under localized notations there's a whole bunch of 56 00:03:05,464 --> 00:03:07,630 different formats supported. 57 00:03:07,630 --> 00:03:08,550 Some American ones. 58 00:03:08,550 --> 00:03:10,200 Some more generic. 59 00:03:10,200 --> 00:03:12,070 The format here. 60 00:03:12,070 --> 00:03:13,410 This is a literal string. 61 00:03:13,410 --> 00:03:16,600 So that means it's actually a slash, and not some special meaning. 62 00:03:16,600 --> 00:03:19,090 And then you can see some actual examples of how they look. 63 00:03:19,090 --> 00:03:22,470 So, when you use them they'll come out without the quotation marks around them. 64 00:03:24,330 --> 00:03:27,590 And there's some other fairly common formats here. 65 00:03:27,590 --> 00:03:29,410 This one, like the four digit year, month, and 66 00:03:29,410 --> 00:03:32,180 day with slashes could be one that you might want to use. 67 00:03:32,180 --> 00:03:36,260 Sometimes you will need to work with dates that are stored in unsupported formats, 68 00:03:36,260 --> 00:03:41,210 for example if a string contains two date segments with numeric values below 13 they 69 00:03:41,210 --> 00:03:43,650 could both be days or months. 70 00:03:43,650 --> 00:03:47,520 Luckily the date time class has a method called create from format we can 71 00:03:47,520 --> 00:03:50,830 provide our custom input format to this method along with our date so 72 00:03:50,830 --> 00:03:52,700 that PHP can read the string correctly. 73 00:03:52,700 --> 00:03:57,500 If we go back to our workspace, paste this code in. 74 00:03:57,500 --> 00:04:01,360 On line three here we have a string containing an unsupported date format. 75 00:04:01,360 --> 00:04:05,710 We store it in the raw variable and pass it to the create from format method. 76 00:04:05,710 --> 00:04:06,420 If all goes well, 77 00:04:06,420 --> 00:04:10,890 when we run this once again, we should see our date output in the same format. 78 00:04:10,890 --> 00:04:15,199 This time the date should be 11/10/1968. 79 00:04:16,490 --> 00:04:18,050 Save that and see if it works. 80 00:04:20,050 --> 00:04:20,590 Perfect! 81 00:04:20,590 --> 00:04:21,090 Let's move on.