PrimitiveType

Dates in PHP: Part 2 - strtotime()


When handling dates in PHP, one of the most useful functions available to you is strtotime(). This function parses a string representation of a date and/or time, and returns a timestamp based on it. In this article, which is a follow up to Dates in PHP: Part 1, we look at some of the handy things you can do with this function!

Note: in the examples below I use the date() function to format the timestamps as easy to read dates.

To begin with, we'll get a timestamp for the current second:

<?php $t = strtotime('now'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-08 13:05:09

This isn't very useful because we can just use the time() function to get the same timestamp. However, strtotime() makes getting a timestamp relative to the current time very simple. We'll start with passing the string "today" to the function; what it returns is the timestamp for today at 00:00:00:

<?php $t = strtotime('today'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-08 00:00:00

Similarly, we can use "yesterday" and "tomorrow" to get corresponding timestamps at 00:00:00 on the relevant dates:

<?php $t = strtotime('yesterday'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-07 00:00:00 $t = strtotime('tomorrow'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-09 00:00:00

To get a timestamp that is a number of days before or after the current time, we can use plus ("+") and minus ("-") and the number of days:

<?php $t = strtotime('-2 days'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-06 13:32:54 $t = strtotime('+2 days'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-10 13:32:54

We can do a similar thing with minutes and seconds:

<?php $t = strtotime('-5 minutes'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-08 13:40:17 $t = strtotime('+5 minutes'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-08 13:50:17 $t = strtotime('-30 seconds'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-08 13:44:47 $t = strtotime('+30 seconds'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-08 13:45:47

And similarly, we can add or subtract months and years from the current date:

<?php $t = strtotime('-1 month'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-04-08 16:57:20 $t = strtotime('+1 month'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-06-08 16:57:20 $t = strtotime('-1 year'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2017-05-08 16:57:20 $t = strtotime('+1 year'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2019-05-08 16:57:20

strtotime() also understands relative terms such as "next" and "last", and in this example we combine them with the names of days of the week:

<?php $t = strtotime("next Saturday"); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-12 00:00:00 $t = strtotime("last Monday"); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-07 00:00:00

strtotime() can of course also parse a specific date-time as follows:

<?php $t = strtotime('2018-05-08 15:50:05'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-05-08 15:50:05

And all of the above modifications relative to that date-time can be performed as well. In this example we apply all of them in one go:

<?php $t = strtotime('2018-05-08 15:50:05 +1 year 3 months 4 days 2 hours 3 minutes 30 seconds'); echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2019-08-12 17:53:35

Because we can specify an increase or decrease to a reference date in any of the units already mentioned, strtotime() is useful for help in looping through dates. The hard work of knowing when we have crossed a boundary such as a month or year is done for us, as this example illustrates:

<?php $startDate = '2018-12-28'; for ($day = 0; $day < 7; $day++) { $t = strtotime("$startDate +$day days"); echo date('Y-m-d H:i:s', $t) . PHP_EOL; } /* Prints: 2018-12-28 00:00:00 2018-12-29 00:00:00 2018-12-30 00:00:00 2018-12-31 00:00:00 2019-01-01 00:00:00 2019-01-02 00:00:00 2019-01-03 00:00:00 */

strtotime() is useful in handling leap years too:

<?php $startDate = '2020-02-28'; for ($day = 0; $day < 3; $day++) { $t = strtotime("$startDate +$day days"); echo date('Y-m-d H:i:s', $t) . PHP_EOL; } /* Prints: 2020-02-28 00:00:00 2020-02-29 00:00:00 2020-03-01 00:00:00 */

A more robust handling of Daylight Saving Time (DST) is also possible with strtotime(), when compared with just adding the number of seconds in a day, 86400, to a timestamp. In this example we are trying to advance the $t timestamp variable by one day, starting on the 28th of October, which is when Daylight Saving Time ends in Europe in 2018:

<?php $startDate = '2018-10-28'; $t = strtotime("$startDate") + 86400; // we use strtotime() to create the base timestamp but add 86400 to it echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-10-28 23:00:00 - one hour behind, we were expecting it to be 29th Oct 2018 $t = strtotime("$startDate +1 day"); // we add the extra day from within strtotime() call echo date('Y-m-d H:i:s', $t) . PHP_EOL; // 2018-10-29 00:00:00 - 29th Oct 2018 as expected

This can reduce unexpected behavior in applications that do not explicitly consider DST.

The above examples will hopefully have convinced you to make use of strtotime() over pure arithmetic on timestamps. It will reduce the number of bugs you introduce into your code and save you time too. Another option would be to use PHP's DateTime class, and perhaps that will be the basis for a "Dates in PHP: Part 3" article!