Comparing DateTime objects

- If you have two DateTime objects and you need…to find out which date and time is earlier than the other,…you can compare them directly…with the normal comparison operators.…This file contains two DateTime objects,…$date1 and $date2, and then there's a series…of conditional statements that compares them.…The first condition uses the identity operator.…That's ===.…This will return true only if both variables…point to the same DateTime object.…

The second condition uses the equality operator, ==.…This returns true if both objects…represent the same date and time.…The third condition uses the < operator…which returns true if $date1 represents…a date and time that's earlier than $date2.…And the final condition uses the > operator…which returns true if $date1 represents…a date and time later than $date2.…At the moment, $date1 and $date2 are initialized…without passing an argument to the constructor,…so they both represent the current date and time.…

And if we test the page in the browser,…the conditional statement confirms that $date1…

Resume Transcript Auto-Scroll

Author

Updated

7/8/2016

Released

3/3/2015

Need to calculate the date of the third Thursday of each month or the Friday after next? PHP—and David Powers—have you covered. David shows how to use the intricate and sometimes underused capabilities of PHP's date and time functions, including the basic date/time formatting functions in PHP 4 and the more advanced functions introduced in PHP 5. For example, you can learn how to find and set time zones, perform date calculations that incorporate different time zones and daylight savings, and calculate the time of sunrises and sunsets. Start watching and learn how to incorporate these powerful functions into your programming arsenal.