如何从 PHP 中的日期时间对象中减去 24 小时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17718107/
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-25 16:19:51  来源:igfitidea点击:

how do I subtract 24 hour from date time object in PHP

phpdatetime

提问by adit

I have the following code:

我有以下代码:

  $now = date("Y-m-d H:m:s");
  $date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));

However, now it gives me this error:

但是,现在它给了我这个错误:

A non well formed numeric value encountered in...

why is this?

为什么是这样?

回答by vascowhite

$date = (new \DateTime())->modify('-24 hours');

or

或者

$date = (new \DateTime())->modify('-1 day');

(The latter takes into account this commentas it is a valid point.)

(后者考虑了这个评论,因为它是一个有效的观点。)

Should work fine for you here. See http://PHP.net/datetime

在这里应该适合你。见http://PHP.net/datetime

$date will be an instance of DateTime, a real DateTime object.

$date 将是DateTime 的一个实例,一个真正的 DateTime 对象。

回答by Joel Harkes

strtotime()expects a unix timestamp (which is number seconds since Jan 01 1970)

strtotime()需要一个 unix 时间戳(即number seconds since Jan 01 1970

$date = date("Y-m-d H:i:s", strtotime('-24 hours', time())); ////time() is default so you do not need to specify.

i would suggest using the datetime library though, since it's a more object oriented approach.

不过,我建议使用 datetime 库,因为它是一种更面向对象的方法。

$date = new DateTime(); //date & time of right now. (Like time())
$date->sub(new DateInterval('P1D')); //subtract period of 1 day

The advantage of this is that you can reuse the DateInterval:

这样做的好处是您可以重用DateInterval

$date = new DateTime(); //date & time of right now. (Like time())
$oneDayPeriod = new DateInterval('P1D'); //period of 1 day
$date->sub($oneDayPeriod);
$date->sub($oneDayPeriod); //2 days are subtracted.
$date2 = new DateTime(); 
$date2->sub($oneDayPeriod); //can use the same period, multiple times.

Carbon (update 2020)

碳(2020 年更新)

Most popular library for processing DateTimes in PHP is Carbon.

在 PHP 中处理日期时间的最流行的库是Carbon

Here you would simply do:

在这里,您只需执行以下操作:

$yesterday = Carbon::now()->subDay();

回答by Sumit Bijvani

you can do this in many ways...

你可以通过多种方式做到这一点......

echo date('Y-m-d H:i:s',strtotime('-24 hours')); // "i" for minutes with leading zeros

OR

或者

echo date('Y-m-d H:i:s',strtotime('last day')); // 24 hours (1 day)

Output

输出

2013-07-17 10:07:29

回答by Sundar

This may be helpful for you:

这可能对您有帮助:

//calculate like this
$date = date("Y-m-d H:m:s", (time()-(60*60*24)));

//check the date
echo $date;

回答by Sreedhu Madhu

Simplest way to sub or add time,

最简单的方法来减少或增加时间,

<?php
**#Subtract 24 hours**
$dtSub = new DateTime('- 24 hours');
var_dump($dtSub->format('Y-m-d H:m:s'));
**#Add 24 hours**
$dtAdd = new DateTime('24 hours');
var_dump($dtAdd->format('Y-m-d H:m:s'));die;
?>

回答by Dipen Soni

$now = date("Y-m-d H:i:s");
$date = date("Y-m-d H:i:s", strtotime('-24 hours', strtotime($now)));

Add "strtotime" before $now, and Y-m-d H:m:s replace with Y-m-d H:i:s

在 $now 前添加 "strtotime",并将 Ymd H:m:s 替换为 Ymd H:i:s

回答by steven

this should work, too

这也应该有效

$date = date("Y-m-d H:m:s", strtotime('-24 hours'));

回答by Amal Murali

You can simply use time()to get the current timestamp.

您可以简单地使用time()来获取当前时间戳。

$date = date("Y-m-d H:m:s", strtotime('-24 hours', time()));

回答by Mohamed Salah

all you have to do is to alter your code to be

你所要做的就是改变你的代码

$now = strtotime(date("Y-m-d H:m:s"));
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));