Chapter 17. Dates and Times

The typical PHP developer likely needs to be aware of the date and time functions available to them, 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 be used to handle date and time information at the same time. There is also 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 client tells 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).


The original date (and related) functions have a timing flaw in them (in versions prior to 5.1) on Windows and some Unix installations. They cannot process dates prior to January 1, 1970, or dates 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 better to use the newer DateTime class family for better ...

Get Programming PHP, 3rd 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.