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.
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; ?>
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.
PHP date_diff() Function $date2=date_create("2013-12-12"); $diff=date_diff($date1,$date2);
You can try:
print('Next Date ' . date('Y-m-d', strtotime('-1 day', strtotime($date_raw))));
date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $date) ) ));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With