Subtract 1 day with PHP

时光怂恿深爱的人放手 提交于 2019-11-26 08:12:52

问题


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.


回答1:


You can try:

print('Next Date ' . date('Y-m-d', strtotime('-1 day', strtotime($date_raw))));



回答2:


 date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $date) ) ));



回答3:


$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.




回答4:


Object oriented version

$dateObject = new DateTime( $date_raw );
print('Next Date ' . $dateObject->sub( new DateInterval('P1D') )->format('Y-m-d');



回答5:


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

Online PHP Editor




回答6:


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);



回答7:


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"));



回答8:


How about this: convert it to a unix timestamp first, subtract 60*60*24 (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




回答9:


Simple like that:

date("Y-m-d", strtotime("-1 day"));

date("Y-m-d", strtotime("-1 months"))




回答10:


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);


来源:https://stackoverflow.com/questions/7651263/subtract-1-day-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!