用 PHP 减去 1 天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7651263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:06:00  来源:igfitidea点击:

Subtract 1 day with PHP

phpdatetimeoffset

提问by td-dev

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

我正在尝试从我的 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

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.

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

回答by MasterCassim

You can try:

你可以试试:

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

回答by iThink

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

回答by aberdat

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

使用 DateTime 显着减少了处理日期时所承受的头痛。

回答by Cosmitar

Object oriented version

面向对象版本

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

回答by Armfoot

A one-lineroption is:

一个一行程序选项是:

echo date_create('2011-04-24')->modify('-1 days')->format('Y-m-d');

Running it on Online PHP Editor.

Online PHP Editor上运行它。



mktimealternative

mktime替代品

If you prefer to avoid using string methods, or going into calculations, or even creating additional variables, mktimesupports subtraction and negative values in the following way:

如果您更喜欢避免使用字符串方法,或进行计算,甚至创建额外的变量,mktime以下列方式支持减法和负值:

// 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

在线 PHP 编辑器

回答by Clive

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

回答by cuperto

Answear taken from Php manual strtotime function comments :

Answear 取自 PHP 手册 strtotime 函数注释:

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

回答by user3413723

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.

怎么样:首先将其转换为 unix 时间戳,减去 60*60*24(正好是一天,以秒为单位),然后从中获取日期。

$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

注意:正如 apokryfos 指出的那样,这在技术上会被夏令时更改所抛弃,其中一天有 25 或 23 小时

回答by Militaru

Simple like that:

就这么简单:

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

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

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

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

回答by JIYAUL MUSTAPHA

How to add 1 year to a date and then subtract 1 day from it in asp.net c#

如何在asp.net c#中将1年添加到日期然后从中减去1天

Convert.ToDateTime(txtDate.Value.Trim()).AddYears(1).AddDays(-1);