date() itself is intended only for formatting, but it takes a second parameter.
date("F j, Y", time() - 60 * 60 * 24);
To keep it simple, I simply subtract 24 hours from the unix timestamp.
Modern oop approach uses DateTime
$date = new DateTime(); $date->sub(new DateInterval('P1D')); echo $date->format('F j, Y') . "\n";
Or in your case (more readable / obvious)
$date = new DateTime(); $date->add(DateInterval::createFromDateString('yesterday')); echo $date->format('F j, Y') . "\n";
(Since DateInterval is negative here, we must add() here)
See also: DateTime::sub() and DateInterval
KingCrunch Jul 22 '11 at 10:45 2011-07-22 22:45
source share