How to get date yesterday using php?

I want to get yesterday's date using a specific date format in php, this is the format:

$today = date("dmY"); //15.04.2013 

Is it possible?

Consider the month and years if they need to be changed in the appropriate.

+62
date php
Apr 15 '13 at 6:42
source share
6 answers

there you go

 date('dmY',strtotime("-1 days")); 

this will work if the month changes

+193
Apr 15 '13 at 6:43
source share

Step 1

We need format format data in the date () function: The date () function returns a string formatted according to the given format string using the given timestamp or the current time ifno timestamp. In other words, timestampis is optional and points to the value of time ().

 <?php echo date("F j, Y"); ?> 

Result: March 30, 2010

Step 2

For the "yesterday" date, use the php function mktime (): The mktime () function returns the Unix timestamp corresponding to the given parameters. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1, 1970 00:00:00 GMT) and the specified time. Arguments can be omitted from right to left; any argument omitted will be set to the current value according to localdate and time.

 <?php echo mktime(0, 0, 0, date("m"), date("d")-1, date("Y")); ?> 

Result: 1269820800

Step 3

Now combine everything and look at this:

 <?php $yesterday = date("Ymd", mktime(0, 0, 0, date("m") , date("d")-1,date("Y"))); echo $yesterday; ?> 

Result: March 29, 2010

Acting in the same way, you can get the time back.

 <?php $yesterday = date("H:i:s",mktime(date("H"), 0, 0, date("m"),date("d"), date("Y"))); echo $yesterday; ?> 

Result: 20:00:00

or 7 days ago:

 <?php $week = date("Ymd",mktime(0, 0, 0, date("m"), date("d")-7,date("Y"))); echo $week; ?> 

Result: 2010-03-23

+7
Apr 15 '13 at 6:54
source share

you can do it with

 date("F j, Y", time() - 60 * 60 * 24); 

or

 date("F j, Y", strtotime("yesterday")); 
+5
Apr 15 '13 at 6:43
source share

try it

  $tz = new DateTimeZone('Your Time Zone'); $date = new DateTime($today,$tz); $interval = new DateInterval('P1D'); $date->sub($interval); echo $date->format('dmy'); ?> 
+5
Apr 15 '13 at 7:02
source share

try it

 <?php $yesterday = date("dmY", time()-86400); echo $yesterday; 
+2
Apr 15 '13 at 6:50
source share

You can also do this using the Carbon library:

 Carbon::yesterday()->format('dmY'); // '26.03.2019' 

In other formats:

 Carbon::yesterday()->toDateString(); // '2019-03-26' Carbon::yesterday()->toDateTimeString(); // '2019-03-26 00:00:00' Carbon::yesterday()->toFormattedDateString(); // 'Mar 26, 2019' Carbon::yesterday()->toDayDateTimeString(); // 'Tue, Mar 26, 2019 12:00 AM' 
0
Mar 27 '19 at 15:10
source share



All Articles