Subtract 1 day with PHP
PhpDatetimeoffsetPhp Problem Overview
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.
Php Solutions
Solution 1 - Php
You can try:
print('Next Date ' . date('Y-m-d', strtotime('-1 day', strtotime($date_raw))));
Solution 2 - Php
date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $date) ) ));
Solution 3 - Php
$date = new DateTime("2017-05-18"); // For today/now, don't pass an arg.
$date->modify("-1 day");
echo $date->format("Y-m-d H:i:s");
Using DateTime has significantly reduced the amount of headaches endured whilst manipulating dates.
Solution 4 - Php
Object oriented version
$dateObject = new DateTime( $date_raw );
print('Next Date ' . $dateObject->sub( new DateInterval('P1D') )->format('Y-m-d');
Solution 5 - Php
A one-liner option is:
echo date_create('2011-04-24')->modify('-1 days')->format('Y-m-d');
Running it on Online PHP Editor.
##mktime alternative
If you prefer to avoid using string methods, or going into calculations, or even creating additional variables, mktime supports subtraction and negative values in the following way:
// Today's date
echo date('Y-m-d'); // 2016-03-22
// Yesterday's date
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-1, date("Y"))); // 2016-03-21
// 42 days ago
echo date('Y-m-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('Y-m-d',
mktime(0, 0, 0,
$date_object->format("m"),
$date_object->format("d")-1,
$date_object->format("Y")
)
); // 2011-04-23
Solution 6 - Php
Not sure why your current code isn't working but if you don't specifically need a date object this will work:
$first_date = strtotime($date_raw);
$second_date = strtotime('-1 day', $first_date);
print 'First Date ' . date('Y-m-d', $first_date);
print 'Next Date ' . date('Y-m-d', $second_date);
Solution 7 - Php
You can add strtotime()
in date()
with parameter number of day week or month. Example for day date("Y-m-d", strtotime("-1 day"));
for week date("Y-m-d", strtotime("-1 week"));
for month date("Y-m-d", strtotime("-1 months"));
Solution 8 - Php
Answear taken from Php manual strtotime function comments :
echo date( "Y-m-d", strtotime( "2009-01-31 -1 day"));
Or
$date = "2009-01-31";
echo date( "Y-m-d", strtotime( $date . "-1 day"));
Solution 9 - Php
How about this: convert it to a unix timestamp first, subtract 606024 (exactly one day in seconds), and then grab the date from that.
$newDate = strtotime($date_raw) - 60*60*24;
echo date('Y-m-d',$newDate);
Note: as apokryfos has pointed out, this would technically be thrown off by daylight savings time changes where there would be a day with either 25 or 23 hours
Solution 10 - Php
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);