Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtract 1 day with PHP

I'm trying to take a date object that's coming out of my Drupal CMS, subtract one day and print out both dates. Here's 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,'Y-m-d'));  //this gives me the correctly formatted string '2011-04-24'  print('Next Date ' . date_format($next_date_object,'Y-m-d'));  //this gives me nothing. The output here is always blank 

So I'm not understanding why the original date object is coming out fine, but then I'm trying to create an additional date object and modify it by subtracting one day and it seems like I can't do that. The output always comes out blank.

like image 580
td-dev Avatar asked Oct 04 '11 16:10

td-dev


People also ask

How to subtract 1 day from current date in PHP?

Subtract From the Given Date in PHP$currentDate = new DateTime('2021-01-01'); //Subtract a day using DateInterval $yesterdayTime = $currentDate->sub(new DateInterval('P1D')); //Get the date in a YYYY-MM-DD format. $yesterday = $yesterdayTime->format('Y-m-d'); //Print Date. echo $yesterday; ?>

How to minus date PHP?

The date_diff() is an in-built PHP function used to get the difference between two dates. It returns a DateInterval object on success or false on failure. The date_diff() function accepts two DateTime objects of the two dates being subtracted.

How can I get the difference between two dates in PHP?

PHP date_diff() Function $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2);


2 Answers

You can try:

print('Next Date ' . date('Y-m-d', strtotime('-1 day', strtotime($date_raw)))); 
like image 113
MasterCassim Avatar answered Oct 29 '22 07:10

MasterCassim


 date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $date) ) )); 
like image 36
iThink Avatar answered Oct 29 '22 07:10

iThink