Using strtotime with PHP

Posted in PHP -

The strtotime() function in PHP allows you to convert English text date time strings into UNIX timestamps. It is useful for converting database datetime strings into UNIX timestamps and also creating dates into the future or past based on the current time or relative to a date and time in the future or the past. This post looks at some examples of doing this.

The PHP function strtotime() has the following usage:

int strtotime ( string $time [, int $now ] )

This means that you pass in a string value for the time, and optionally a value for the current time, which is a UNIX timestamp. The value that is returned is an integer which is a UNIX timestamp.

An example of this usage is as follows, where the date passed to strtotime() might be a date from a database query or similar:

$ts = strtotime('2007-12-21');

This will return into the $ts variable the value 1198148400, which is the UNIX timestamp for the date December 21st 2007. This can be confirmed using the date() function like so:

echo date('Y-m-d', 1198148400);
// echos 2007-12-21

strtotime() is able to parse a wide variety of strings and convert them to the appropriate timestamp, using actual dates and also strings such as "next week", "next tuesday", "last thursday", "2 weeks ago" and so on. Here are some examples:

$ts = strtotime('21 december 2007');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

This will display the following:

1198148400
2007-12-21

If today is December 21st, then the following:

$ts = strtotime('next week');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

$ts = strtotime('next tuesday');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

$ts = strtotime('last thursday');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

$ts = strtotime('2 weeks ago');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

$ts = strtotime('+ 1 month');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

will display the following:

1199006542
2007-12-30
1198494000
2007-12-25
1198062000
2007-12-20
1197192142
2007-12-09
1201080142
2008-01-23

Using strtotime to offset from a different date

If you want to use the PHP function strtotime to add or subtract a number of days, weeks, months or years from a date other than the current time, you can do it by passing the second optional parameter to the strtotime() function, or by adding it into the string which defines the time to parse.

This example shows passing the second parameter. Doing it this way requires that the date to offset from is already a UNIX timestamp. In this example the timestamp being passed is December 25th 2007 (Christmas Day).

$ts = strtotime('tomorrow', 1198494000);
echo $ts, '<br />';
echo date('Y-m-d', $ts);

The result would be:

1198580400
2007-12-26

As you can see, 'tomorrow' has been parsed as tomorrow starting from the 1198494000 timestamp, which is December 25th 2007, resulting in a timestamp being returned as December 26th 2007.

Another way to add or subtract time from an existing time string is to add it into the datetime string like so, where we are adding 90 days on to a date value, in the format of the MySQL date field:

$ts = strtotime('2007-10-12 +90 days');
echo $ts, '<br />';
echo date('Y-m-d', $ts), '<br />';

This would output:

1199876400
2008-01-10

As you can see the strtotime() function is extremely useful for parsing English (and databases) representations of date and time strings and turning them into UNIX timestamps. It is also useful for adding plus and minus offsets to those timestamps to easily create dates and times in the future and past.



Related posts:


Comments