Dates and Times

One of the most common things I seem to do is manipulate dates and times in some way, PHP is no different. The following code will get the current date/time and store it in a variable:
$today = getdate();
However, if you have not configured PHP properly, specifically, created a php.ini file, then you will get a warning, in the form of a notice saying you are required to set a timezone rather than rely on the system default. So you have a choice, either add date.timezone = Europe/London in the "[Date]" section of your php.ini file, or call date_default_timezone_set("Europe/London"); before using any date or time functions. There is a list of valid timezones at PHP: List of Supported Timezones - Manual if you don't live in the UK.

My conclusion on timezones is that whatever you do, PHP understands what UTC is and translates, hence when I use a machine in Germany, but specify a UK timezone, then I get UK time not European time or some kind of mess.

There is a full explanation of date and time functions at PHP: Date and Time Related Extensions - Manual which also includes Calendar functions.


If you read PHP: strftime - Manual you will notice some comments about different platforms. In summary this function delegates to the OS and so if the underlying OS does not support the format neither does PHP, on that platform! The best part is that strftime returns nothing if it finds an unsupported option, so be warned! A good, cross platform way of getting the current date/time is by using strftime("%Y-%m-%d %H:%M:%S").

See also