1 00:00:00,590 --> 00:00:02,300 Now we know how to make a date time object, 2 00:00:02,300 --> 00:00:03,570 we should learn how to do things with them. 3 00:00:03,570 --> 00:00:07,620 One of the first things we might like to do is compare dates. 4 00:00:07,620 --> 00:00:10,500 Date comparisons are made very easy in PHP. 5 00:00:10,500 --> 00:00:13,540 Many older tutorials will suggest you have to turn all dates into Unix time 6 00:00:13,540 --> 00:00:16,040 stamps before you can compare the number of seconds, but 7 00:00:16,040 --> 00:00:17,920 date time objects can be compared directly. 8 00:00:19,730 --> 00:00:22,990 Try and steer clear of the old date, and STR time functions. 9 00:00:22,990 --> 00:00:25,630 And definitely avoid adding and subtracting specific numbers of 10 00:00:25,630 --> 00:00:29,400 seconds to change dates due to time zone complications. 11 00:00:29,400 --> 00:00:31,060 Let's take a look at time zones in the next video. 12 00:00:33,030 --> 00:00:35,310 Let's open the index.php file in our work space now. 13 00:00:35,310 --> 00:00:39,850 And have a look at another handy feature of date time that is often under utilized. 14 00:00:39,850 --> 00:00:43,480 On lines three and four, we are creating two new date time objects, 15 00:00:43,480 --> 00:00:46,260 both of which contain dates in their supported format. 16 00:00:46,260 --> 00:00:48,490 On line three, there is an object named dvone. 17 00:00:48,490 --> 00:00:51,600 And on line four contains an object named spike. 18 00:00:51,600 --> 00:00:54,590 These daytime objects represent the birthdays of famous 19 00:00:54,590 --> 00:00:57,500 WWE wrestlers D-Von Dudley and Spike Dudley. 20 00:00:57,500 --> 00:00:59,860 Below that on line eight is an if statement. 21 00:00:59,860 --> 00:01:04,430 This if statement is comparing which is lesser out of the two objects. 22 00:01:04,430 --> 00:01:08,180 At first it might seem odd to compare two objects directly, but when this is used, 23 00:01:08,180 --> 00:01:10,880 the internal values of the date, regardless of the format they were 24 00:01:10,880 --> 00:01:14,920 created with will be compared to see which is the lesser of the two. 25 00:01:14,920 --> 00:01:15,820 Think about it. 26 00:01:15,820 --> 00:01:19,700 If somebody is born in 1900, then that is a small number than 2000. 27 00:01:19,700 --> 00:01:23,650 And somebody born in 1900 is certainly older than someone born in 2000. 28 00:01:23,650 --> 00:01:26,530 When we run this we should expect to see the paragraph Spike is 29 00:01:26,530 --> 00:01:28,750 older than D-Von output. 30 00:01:28,750 --> 00:01:31,840 As Spike was born in 1970, almost two years before D-Von. 31 00:01:34,380 --> 00:01:36,800 Great our if statement worked, and we know who is older. 32 00:01:36,800 --> 00:01:37,920 Comparisons are useful, but 33 00:01:37,920 --> 00:01:40,570 we can also look at the actual difference between the two dates. 34 00:01:40,570 --> 00:01:42,950 This will let us see what the age gap really is. 35 00:01:42,950 --> 00:01:45,380 So we go back to our workspace here. 36 00:01:45,380 --> 00:01:46,370 Uncomment these lines. 37 00:01:48,430 --> 00:01:49,800 Refresh that. 38 00:01:49,800 --> 00:01:50,820 There we have it. 39 00:01:50,820 --> 00:01:54,830 There is one year, 11 months, and 19 days between Spike and D-Von. 40 00:01:54,830 --> 00:01:56,310 But how do we do that. 41 00:01:56,310 --> 00:02:00,700 On line 16, we are creating an object and storing it in a variable called diff. 42 00:02:00,700 --> 00:02:03,670 This object is an instance of date time interval. 43 00:02:03,670 --> 00:02:05,250 And that means we can call methods on it. 44 00:02:05,250 --> 00:02:07,010 One method we recall is the format method. 45 00:02:07,010 --> 00:02:09,498 This is a little different to the DateTime Format method, but 46 00:02:09,498 --> 00:02:10,730 it works in much the same way. 47 00:02:10,730 --> 00:02:14,680 We can pass it as string and use certain characters preceded by a percentage sign 48 00:02:14,680 --> 00:02:18,570 to denote actual values that represent a portion of the difference. 49 00:02:18,570 --> 00:02:22,050 For example, we have a percent y, which will show the number of years, 50 00:02:22,050 --> 00:02:24,500 a percent m, which shows the number of months. 51 00:02:24,500 --> 00:02:27,780 And percent d, which you guessed it, shows the number of days. 52 00:02:27,780 --> 00:02:30,930 That's a good introduction to DateTime and comparisons. 53 00:02:30,930 --> 00:02:32,900 So, now we're gonna move on to time zones.