PHP date yesterday

Possible duplicate:
Get timestamp today and yesterday in php

I was wondering if there was an easy way to get yesterday's date in this format:

date("F j, Y"); 
+83
date php
Jul 22 2018-11-22T00:
source share
3 answers

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

+174
Jul 22 '11 at 10:45
source share

strtotime() , as in date("F j, Y", strtotime("yesterday"));

+113
Jul 22 2018-11-22T00:
source share

How easy :)

 date("F j, Y", strtotime( '-1 days' ) ); 
+80
Sep 25 '12 at 9:19
source share



All Articles