3v4l.org

run code in 300+ PHP versions simultaneously
<?php $year=2021; $month=7; $day=5; $index=-1; $period='day'; // strtotime test $dateString = $year . "-" . $month . "-" . $day . " -" . $index . " " . $period; $timestamp1 = strtotime("{$year}-{$month}-{$day} -{$index} {$period}"); // DateTime test $dateStamp = new \DateTime("{$year}-{$month}-{$day}"); $dateStamp->modify("-{$index} {$period}"); $timestamp2 = $dateStamp->getTimeStamp(); echo $dateString . PHP_EOL; echo "strtotime: " . $timestamp1 . PHP_EOL; echo "DateTime: " . $timestamp2;
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.25 - 8.2.29, 8.3.13 - 8.3.25, 8.4.1 - 8.4.12
2021-7-5 --1 day strtotime: 1625522400 DateTime: 1625522400
Output for 8.2.0 - 8.2.24, 8.3.0 - 8.3.12
2021-7-5 --1 day strtotime: 1625349600 DateTime: 1625349600

preferences:
95.75 ms | 407 KiB | 5 Q