Chapter 7. Dates and Times
The typical PHP developer likely needs to be aware of the available date and time functions, such as when adding a date stamp to a database record entry or calculating the difference between two dates. PHP provides a DateTime
class that can handle both date and time information simultaneously, as well as a DateTimeZone
class that works hand in hand with it.
Time zone management has become more prominent in recent years with the onset of web portals and social web communities like Facebook and Twitter. To be able to post information to a website and have it recognize where you are in the world in relation to others on the same site is definitely a requirement these days. However, keep in mind that a function like date()
takes the default information from the server on which the script is running, so unless the human clients tell you where they are in the world, it can be quite difficult to determine time zone location automatically. Once you know the information, though, it’s easy to manipulate that data (more on time zones later in this chapter).
Note
The original date (and related) functions contain a timing flaw on Windows and some Unix installations. They cannot process dates prior to December 13, 1901, or beyond January 19, 2038, due to the nature of the underlying 32-bit signed integer used to manage the date and time data. Therefore, it is recommended to use the newer DateTime
class family for better accuracy going forward.
There are four interrelated ...
Get Programming PHP, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.