Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
DateTime Comparisons2:33 with Phil Sturgeon
In the olden days, PHP developers converted everything to unix timestamps before comparing dates, but now using DateTime you can just compare the objects directly.
Now we know how to make a date time object, 0:00 we should learn how to do things with them. 0:02 One of the first things we might like to do is compare dates. 0:03 Date comparisons are made very easy in PHP. 0:07 Many older tutorials will suggest you have to turn all dates into Unix time 0:10 stamps before you can compare the number of seconds, but 0:13 date time objects can be compared directly. 0:16 Try and steer clear of the old date, and STR time functions. 0:19 And definitely avoid adding and subtracting specific numbers of 0:22 seconds to change dates due to time zone complications. 0:25 Let's take a look at time zones in the next video. 0:29 Let's open the index.php file in our work space now. 0:33 And have a look at another handy feature of date time that is often under utilized. 0:35 On lanes three and four, we are creating two new date time objects, 0:39 both of which contain dates in their supported format. 0:43 On lane three, there is an object named D-Von. 0:46 And on lane four contains an object named Spike. 0:48 These daytime objects represent the birthdays of famous 0:51 WWE wrestlers D-Von Dudley and Spike Dudley. 0:54 Below that on line eight is an if statement. 0:57 This if statement is comparing which is lesser out of the two objects. 0:59 At first it might seem odd to compare two objects directly, but when this is used, 1:04 the internal values of the date, regardless of the format they were 1:08 created with will be compared to see which is the lesser of the two. 1:10 Think about it. 1:14 If somebody is born in 1900, then that is a small number than 2000. 1:15 And somebody born in 1900 is certainly older than someone born in 2000. 1:19 When we run this we should expect to see the paragraph Spike is 1:23 older than Devon output. 1:26 As Spike was born in 1970, almost two years before Devon. 1:28 Great our if statement worked, and we know who is older. 1:34 Comparisons are useful, but 1:36 we can also look at the actual difference between the two dates. 1:37 This will let us see what the age gap really is. 1:40 So we go back to our workspace here. 1:42 Uncomment these lines. 1:45 Refresh that. 1:48 There we have it. 1:49 There is one year, 11 months, and 19 days between Spike and Devon. 1:50 But how do we do that. 1:54 On line 16, we are creating an object and storing it in a variable called def. 1:56 This object is an instance of date time interval. 2:00 And that means we can call methods on it. 2:03 One method we recall is the format method. 2:05 This is a little different to the data informant method, but 2:07 it works in much the same way. 2:09 We can pass it as string and use certain characters preceded by a percentage sign 2:10 to denote actual values that represent a portion of the difference. 2:14 For example, we have a percent y, which will show the number of years, 2:18 a percent m, which shows the number of months. 2:22 And percent d, which you guessed it, shows the number of days. 2:24 That's a good introduction to day time and comparisons. 2:27 So, now we're gonna move on to time zones. 2:30
You need to sign up for Treehouse in order to download course files.Sign up