Join David Powers for an in-depth discussion in this video Introducing the DateTime class, part of PHP Date and Time Essential Training.
- In this chapter, we'll take an in-depth look at the DateTime class, which was introduced in PHP 5.2. The class has multiple advantages over the functions inherited from PHP 4. The main advantage is that DateTime objects store not only the date and time, they also store details of the time zone. More important, the time-zone information is independent of the server's default time zone. You can create DateTime objects for different time zones, or change the time zone of an existing object.
Even on a 32-bit system, DateTime objects can store what's effectively an infinite range of dates. They're capable of representing dates more than 290 billion years in the past, and into the future. They can parse the same English date and time strings as the string to time function, making them very flexible. Unlike string to time, you can find out why a specific string failed to parse correctly. DateTime objects have dedicated methods for performing date and time calculations.
The modify method changes the timestamp using a string in the same way as string to time. The add and sub methods alter the currently-stored timestamp, by adding or subtracting a specific period. And the diff method calculates the difference between two DateTime objects. Because the objects store time-zone information, this greatly simplifies calculations; automatically taking leap years, and daylight savings time into account.
So what are the disadvantages? There aren't many, but the main one is that using a DateTime object can sometimes involve more code than using one of the functions inherited from PHP 4. For example, it's much simpler to use date to display the current date like this. The equivalent DateTime code looks like this. As this example demonstrates, you format the values stored in DateTime with the same formatting characters as the original date function.
Whether this is an advantage or disadvantage, depends on your viewpoint. In one sense, it's an advantage, because there's no need to learn new formatting characters. But it's a disadvantage, because they're still difficult to memorize. You create an instance of the DateTime class like any other object using the new keyword. And you call methods using the arrow operator like this. The DateTime class and each of its methods, has an equivalent procedural function.
To achieve the same output with procedural functions, you use the date_create function. This returns a DateTime object, which is stored here as now. The date_format function formats the date using the same formatting characters. But it requires the DateTime object as its first argument. Both produce exactly the same result. The procedural functions are simply aliases of the class methods. To avoid confusion, I'll use only the DateTime class and its methods throughout this course, because the code is simpler, and cleaner.
- How PHP handles dates and time
- Setting the default time zone
- Displaying current date and time
- Getting a timestamp from text
- Using the DateTime class
- Getting and modifying a timestamp from DateTime
- Comparing DateTime objects
- Working with time zones
- Using DateInterval and DatePeriod
- Finding ISO week dates
- Calculating sunrise and sunset
Skill Level Intermediate
Q: How does calculating sunrise and sunset (as shown in chapter 7) work for locations north of the Arctic Circle?
A: In polar regions, date_sun_info() reports the value for sunrise and sunset as 1 on days when the sun never sets. The values are empty for days when the sun never rises.