Using Dates and Time in PHP

Using Dates and Time in PHP

John Hewick

What may seem straightforward can often spiral out of control when it comes to dealing with dates and time. The conventional set up may be a piece of cake but start adding different time zones and it can quickly become a head ache.

Thankfully, PHP boasts an unbelievable array of tools and solutions for any time-related issues. From unix timestamps to switching between zones and other more complex functions, PHP offers the freedom to cover all your date and time requirements. Here’s how to go about completing some of the main functions.

Getting the current time

‘time()’ is one of the fundamental functions of PHP’s time programming. For the purpose of this article, everything is referred to in Unix time, otherwise known as POSIX.

In order to get the current unix time, you’ll have to utilise an interactive CLI shell with the following:

example@belementary:~$ php -a

php > print time();

1324402770

For an accurate representation of the Unix time, simply use the ‘getdate()’ function. This opts for an optional Unix timestamp argument but if that isn’t provided will instead default to the ‘time()’ function:

php > $unixTime = time();

php > print_r(getdate($unixTime));

Array

(

[seconds] => 48

[minutes] => 54

[hours] => 12

[mday] => 20

[wday] => 2

[mon] => 12

[year] => 2011

[yday] => 353

[weekday] => Tuesday

[month] => December

[0] => 1324403688

)

Formatting the time

The great thing with Unix time is that it can be formatted with ease into any layout that you desire. The function ‘date()’ is used to format Unix timestamps into readable code. This utilises a formatting argument and optional time argument which if not provided, automatically defaults to ‘time()’:

php > print date(“r”, $unixTime);

Tue, 20 Dec 2011 12:54:48 -0500

Time formats will be determined by RFC 2822 but if you’d prefer to create your own, you can easily make a custom format. Here’s an example:

php > print date(“m/d/y h:i:s a”, $unixTime);

12/20/11 12:54:48 pm

php > print date(“m/d/y h:i:s a”);

12/20/11 01:12:11 pm

php > print date(“jS of F Y”, $unixTime);

20th of December 2011

Using a pre-determined time to create a Unit Time

The function ‘mktime()’ can be utilised to create a Unix timestamp which corresponds to a list of values including seconds, minutes and hours. In order to do this, it requires standalone arguments to establish each part of the date:

mktime(hour, minute, second, month, day, year, isDST)

Always set the ‘isDST’ to ‘1’ if daylight savings is ‘0’. Otherwise, set this as ‘-1’ if it’s unknown which is the default setting.

php > print date(“r”, mktime(12, 0, 0, 1, 20, 1987));

Tue, 20 Jan 1987 12:00:00 -0500

php > print date(“r”, mktime(0, 0, 0, date(“n”), date(“j”), date(“Y”)));

Tue, 20 Dec 2011 00:00:00 -0500

php > print date(“r”, mktime(23, 59, 59, date(“n”), date(“j”), date(“Y”)));

Tue, 20 Dec 2011 23:59:59 -0500

The ‘mktime()’ is clearly very effective and can be especially impressive when overcoming database queries that use customised date ranges. Anyone storing timestamps as integers can easily set up a year to date range.

<?php

$startTime = mktime(0, 0, 0, 1, 1, date(“y”));

$endTime   = mktime(0, 0, 0, date(“m”), date(“d”), date(“y”));

Utilising DateTime and DateTimeZone Objects

For a more object-orientated approach, some may prefer PHP’s ‘DateTime’ function. As with ‘strtotime’ this method uses a string representation of a time:

php > $dt = new DateTime(“now”);

php > print $dt->format(“r”);

Tue, 20 Dec 2011 16:28:32 -0500

php > $dt = new DateTime(“December 31 1999 12:12:12 EST”);

php > print $dt->format(“r”);

Fri, 31 Dec 1999 12:12:12 -0500

The ‘DateTime’s’ object operates in a similar way to the function above, accepting all the same formatting characters. This also comes with some constants that can be added to the ‘format()’ method:

php > print $dt->format(DATE_ATOM);

2011-12-20T15:57:45-05:00

php > print $dt->format(DATE_ISO8601);

2011-12-20T15:57:45-0500

php > print $dt->format(DATE_RFC822);

Tue, 20 Dec 11 15:57:45 -0500

php > print $dt->format(DATE_RSS);

Tue, 20 Dec 2011 15:57:45 -0500

In order to give the PHP a default time zone, you’ll first need to find the following section in your php.ini configuration file:

[Date]

; Defines the default timezone used by the date functions

; http://php.net/date.timezone

; date.timezone =

PHP always refers to the server in order to determine a system time zone when no valued is added to ‘date.timezone’. To check the value which PHP is using, make use of the ‘date_default_timezone_get().’

php > print date_default_timezone_get();

America/New_York

Be sure to set the time zone of the server to UTC time by using ‘date.timezone = UTC’ before saving the configuration. For the changes to be implemented, restart Apache or the CLI.

The PHP ‘DateTime’ objects track time zones through the ‘DateTimeZone’ class. Whenever creating a new instance of ‘DateTime’ be sure to set the ‘DateTimeZone’ to the default provided in ‘php.ini:

php > $dt = new DateTime();

php > print $dt->getTimeZone()->getName();

UTC

From here, you’ll now be able to see the differences in times when two ‘DateTime’ objects are given different time zones. As an example, this preset converts from UTC to America (EST):

php > $dt = new DateTime();

php > print $dt->format(“r”);

Tue, 20 Dec 2011 20:57:45 +0000

php > $tz = new DateTimeZone(“America/New_York”);

php > $dt->setTimezone($tz);

php > print $dt->gt;format(“r”);

Tue, 20 Dec 2011 15:57:45 -0500

As you can see, there’s an -0500 offset in December but if you change the value to that in the summer, it makes allowance for Daylight Savings Time.

php > $tz = new DateTimeZone(“America/New_York”);

php > $july = new DateTime(“7/1/2011”);

php > $july->setTimezone($tz);

php > print $july->>format(“r”);

Thu, 30 Jun 2011 20:00:00 -0400

Shown here are just a handful of the impressive time and date features using PHP. There’s plenty more customisation with endless setup and options to suite your desired outcome.

John Hewick

Author John Hewick

More posts by John Hewick