|
Dates in PHP: Part 2 - strtotime()Posted: Apr 25, 2018 Last modified: May 9, 2018When handling dates in PHP, one of the most useful functions available to you is Note: in the examples below I use the
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 <?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
<?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
<?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
*/
<?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 <?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 |