PHP Dates and Times

In PHP the current date and time can be assigned to a variable as show below. This utilises the 'DateTime' class. Classes are a concept of Object-Oriented Programming discussed later.

$dateTime = new DateTime();

It is also possible to assign a specific date and time to a variable by specifying it within the parenthesis.

$dateTime = new DateTime('2017-02-01 05:30:00');

Here, the date, 1 February 2017, is specified backwards, followed by the time, which is 5:30am. As an alternative, the date and time can be set by using the 'setDate' and 'setTime' methods.

$dateTime = new DateTime();
$dateTime->setDate(2017, 2, 1);
$dateTime->setTime(5, 30, 0);

For 'setDate', the values in parenthesis refer to the year, month and day, and for 'setTime', the values refer to hours, minutes and seconds.

To display the date and time from the variable it is simply a case of using an 'echo' or 'print' statement, along with the format method. 

echo $dateTime->format('d/m/y H:i:s');

The format method uses format characters to specify how the date and time should be displayed. Here, the 'd' refers to a two digit day, the 'm' is a two digit month and the 'y' is a two digit year. This is followed by the time, using a twenty four hour clock, with the hours, minutes and seconds separated by colons. The resulting output is shown below.

01/02/17 05:30:00

The following table shows all of the format characters that can be used when displaying dates and times.

Format
Character
Description
d Day of the month as a number from 01 through 31.
j Day of the month as a number from 1 through 31.
D Three character abbreviation for the day of the week e.g. Mon for Monday.
l Full name for the day of the week e.g. Monday.
m Month as a number, with a leading zero e.g. 03 for March.
n Month as a number, with no leading zero e.g. 3 for March.
M Three character abbreviation for the month e.g. Mar for March.
F Full name for the month e.g. March.
y Year, without the century but includes a leading zero e.g. 09.
Y Year, including the century e.g. 2017.
g Hours, using a 12 hour clock, with no leading zero e.g. 5.
h Hours, using a 12 hour clock, with a leading zero e.g. 05.
G Hours, using a 24 hour clock, with no leading zero e.g. 17.
H Hours, using a 24 hour clock, with a leading zero e.g. 20.
a Time of day, am or pm, in lower case.
A Time of day, AM or PM, in upper case.
i Minutes, with a leading zero e.g. 06.
s Seconds, with a leading zero e.g. 08.

Manipulating Dates and Times

There are a number of useful methods that aid in the manipulation of dates and times. The first of which is the 'add' method, that can add to any component of a date and time. The example below demonstrates how one can be added to each component of a date and time by using 'DateInterval'.

$dateTime = new DateTime('2017-02-01 05:30:00');
echo $dateTime->format('d/m/y H:i:s');

echo '<br/>';

$dateTime->add(new DateInterval('P1Y1M1DT1H1M1S'));
echo $dateTime->format('d/m/y H:i:s');

This assigns the specified date and time to a variable and displays it in the chosen format. 'DateInterval' is then used to add one to each component of the date and time, which is then displayed on the following line in the same format.

01/02/17 05:30:00
02/03/18 06:31:01

The alphanumeric characters within the parenthesis, after 'DateInterval', are what dictates which components in the date and time get incremented and by how much. This must always start with a 'P', which stands for 'Period', regardless of which date and time components are updated. The 'T' denotes the start of the time elements and need only be present if there are time elements. After the 'P' and the 'T' there are number and letter pairings, with the number stating the amount to increment the following date and time component by, for example, '1Y', specifies that the year element of the date should be incremented by one.

The table below shows the date and time elements, or 'Period Designators', as they are sometimes known, together with their meaning.

Period
Designator
Description
Y Years
M Months (Before the 'T' only)
D Days
W Weeks
H Hours
M Minutes (After the 'T' only)
S Seconds

It is only necessary to include the Period Designators which need to be incremented, so if, for example, only the months and hours need incrementing by two, it will look like this.

$dateTime->add(new DateInterval('P2MT2H'));

 As well as the 'add' method, there is also a 'sub' method, which can be used to subtract from the date and time components and works in the same way to 'add'. 

$dateTime = new DateTime('2017-02-01 05:30:00');
echo $dateTime->format('d/m/y H:i:s');

echo '<br/>';

$dateTime->sub(new DateInterval('P3DT4M'));
echo $dateTime->format('d/m/y H:i:s');

Here, three days and four minutes are taken away from the date and time. The month has also changed because by subtracting three days it has gone in to the previous month.

01/02/17 05:30:00
29/01/17 05:26:00

The final method to be discussed here is 'diff', which calculates the difference between two dates.

$dateTimeStart = new DateTime('2017-02-01 05:30:00');
$dateTimeEnd = new DateTime('2017-04-01 05:30:00');
$difference = $dateTimeStart->diff($dateTimeEnd);

echo $difference->format('%R%a days');

Here, the difference in days is displayed between the two date variables, '$dateTimeStart' and '$dateTimeEnd'.

+59 days

In the format, the '%R' is used to display the plus or minus and the '%d' is used to display the days. The word 'days' is just pure text to be displayed.

If it is necessary to display the difference in something other than days then other formats are available, as shown below.

Format
Character
Description
y Years
m Months
a Days
h Hours
i Minutes
s Seconds

These are just some of the date and time methods that are available.

Further Reading