-1

I have this piece of code which sets the date to 360 days later, I would like to know how to change this piece of code to make the date yesterdays date, 28/3/2016

$tmpdate = new DateTime(date("Y-m-d"));
            $tmpdate->add(new DateInterval('P360D'));
            $Available = $tmpdate->format('Y-m-d');
Aurora
  • 282
  • 3
  • 16

2 Answers2

2

You can do that:

date("Y-m-d", strtotime("-1 day")); // yesterday (based on server date)

For any other date you can use strtotime:

strtotime("30-12-9999");

I hope it helps you!

Regards.

Javier Vargas
  • 767
  • 11
  • 18
1

Yesterday:

echo date("Y m d h:i",time() -60*60*24); // 24 hours ago.
echo date("Y m d",time() -60*60*24); // yesterday as in date.

As mark pointed out below I did not think of daylight savings. So...

$date = new DateTime(null, new DateTimeZone('Europe/Stockholm')); // Change to suit
$Heretime = $date->format('H');
$date->setTimezone(new DateTimeZone('UTC'));
$Difftime = $date->format('H') - $Heretime;

echo date("Y m d h:i",time() -60*60*(24-$Difftime)); // 24 hours ago in UTC.
echo date("Y m d",time() -60*60*(24-$Difftime)); // yesterday as in date in UTC.

And from there you can convert it back I guess... Or maybe I'm completely lost?

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • Not if you'e on the cusp of daylight savings.... you're assuming that every day is identical in length – Mark Baker Mar 29 '16 at 10:30
  • True... In that case I guess you need to match the time with UTC time to see if it's daylight savings? – Andreas Mar 29 '16 at 10:35