user2033139 user2033139 - 1 month ago 9
PHP Question

PHP Substracting 1 day from date string php doesnt work

i am trying to substract 1 day from date string, if condition is met.

If condition (if time between 12-16) works fine, but outcome of yesterday's date is wrong.

This is what i tried with research from stackoverflow:

<?php
#trying to remove 1 day from date
$date = date("d/m/y");
if (date('H') < 16 && date('H') > 12) {
$date2 = strtotime(date('d/m/y') . ' -1 day');
$date2 = date('d/m/y', $date2);
}
echo "Todays date is {$date} and yesterday was {$date2}";
?>


Outcome from phpfiddle:


Todays date is 23/10/16 and yesterday was 31/12/69


Can someone share some light on it, how it should be done ?

Answer

instead

$date2 = strtotime(date('d/m/y') . ' -1 day');

use

 $date2 = strtotime('-1 day');