用 PHP 减去1天

我试着从我的 Drupal CMS 中拿出一个日期对象,减去一天,打印出两个日期。这是我手头的资料

$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

所以我不明白为什么原始的日期对象出来的很好,但然后我试图创建一个额外的日期对象,并通过减去一天来修改它,似乎我不能这样做。输出总是空白的。

319795 次浏览

You can try:

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

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);
 date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $date) ) ));

Object oriented version

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

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

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

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

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

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

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