Subtract 1 day with PHP

I am trying to take a date object that comes out of my Drupal CMS, subtract one day and print both dates. Here is what i have

$date_raw = $messagenode->field_message_date[0]['value']; print($date_raw); //this gives me the following string: 2011-04-24T00:00:00 $date_object = date_create($date_raw); $next_date_object = date_modify($date_object,'-1 day'); print('First Date ' . date_format($date_object,'Ym-d')); //this gives me the correctly formatted string '2011-04-24' print('Next Date ' . date_format($next_date_object,'Ym-d')); //this gives me nothing. The output here is always blank 

Therefore, I don’t understand why the original date object fails, but then I try to create an additional date object and change it by subtracting one day, and it seems that I cannot do this. The output is always displayed blank.

+69
php datetimeoffset
04 Oct '11 at 16:45
source share
10 answers

You can try:

 print('Next Date ' . date('Ym-d', strtotime('-1 day', strtotime($date_raw)))); 
+126
04 Oct '11 at 16:51
source share
  date('Ym-d',(strtotime ( '-1 day' , strtotime ( $date) ) )); 
+59
04 Oct '11 at 16:56
source share
 $date = new DateTime("2017-05-18"); // For today/now, don't pass an arg. $date->modify("-1 day"); echo $date->format("Ymd H:i:s"); 

Using DateTime has significantly reduced the number of headaches suffered during date manipulation.

+21
May 18 '17 at 23:09
source share

Object oriented version

 $dateObject = new DateTime( $date_raw ); print('Next Date ' . $dateObject->sub( new DateInterval('P1D') )->format('Ym-d'); 
+19
Aug 6 '13 at
source share

One-liner option:

 echo date_create('2011-04-24')->modify('-1 days')->format('Ym-d'); 

Running it in the online PHP editor .




mktime alternative

If you prefer to avoid using string methods, going to calculations, or even creating additional variables, mktime supports subtraction and negative values ​​as follows:

 // Today date echo date('Ym-d'); // 2016-03-22 // Yesterday date echo date('Ym-d', mktime(0, 0, 0, date("m"), date("d")-1, date("Y"))); // 2016-03-21 // 42 days ago echo date('Ym-d', mktime(0, 0, 0, date("m"), date("d")-42, date("Y"))); // 2016-02-09 //Using a previous date object $date_object = new DateTime('2011-04-24'); echo date('Ym-d', mktime(0, 0, 0, $date_object->format("m"), $date_object->format("d")-1, $date_object->format("Y") ) ); // 2011-04-23 

PHP online editor

+13
Mar 22 '16 at 12:37
source share

Not sure why your current code is not working, but if you don't need a specific object, this will work:

 $first_date = strtotime($date_raw); $second_date = strtotime('-1 day', $first_date); print 'First Date ' . date('Ym-d', $first_date); print 'Next Date ' . date('Ym-d', $second_date); 
+12
Oct 04 '11 at 16:52
source share

The answer is taken from the comments to the php function manual strtotime:

 echo date( "Ymd", strtotime( "2009-01-31 -1 day")); 

or

 $date = "2009-01-31"; echo date( "Ymd", strtotime( $date . "-1 day")); 
+5
Mar 22 '19 at 8:57
source share

How about this: first convert it to a unix timestamp, subtract 60 * 60 * 24 (exactly one day in seconds), and then take the date from it.

 $newDate = strtotime($date_raw) - 60*60*24; echo date('Ym-d',$newDate); 

Note: as apocryphos pointed out, this would technically be discarded by daylight saving time changes when the day comes at 25 or 23 hours.

+3
Jun 11 '15 at 4:58
source share

Just:

date("Ymd", strtotime("-1 day"));

date("Ymd", strtotime("-1 months"))

0
Jul 25 '19 at 17:17
source share

How to add 1 year to a date and then subtract 1 day from it in asp.net c #

 Convert.ToDateTime(txtDate.Value.Trim()).AddYears(1).AddDays(-1); 
-one
Dec 15 '18 at 7:12
source share



All Articles