php - 比给定日期时间早一小时

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

php - One Hour Earlier Than Given Datetime

phpdatetime

提问by foxns7

I'm having a problem here..

我这里有问题..

Supposed I have this kind of datetime.

假设我有这种日期时间。


$date = strtotime($date);

I need this to be converted into the Unix-timestamp format but one hour earlier..I tried

我需要将其转换为 Unix-timestamp 格式,但一小时前..我试过了


$date = strtotime($date-86400)

, but seemed to me it is wrong. It returned 0 when I sneaked into the table.

,但在我看来这是错误的。当我潜入桌子时它返回 0。

What is the appropriate way to achieve this?

实现这一目标的适当方法是什么?

Thanks.

谢谢。

采纳答案by Alexandre D'Eschambeault

strtotime will give you the unix timestamp of your date. From this timestamp, you then subtract your hour:

strtotime 将为您提供日期的 unix 时间戳。从此时间戳中减去您的小时数:

$date = strtotime($date) - 3600; // 3600 = 60 * 60

If you call strtotime($date - 3600), it's like obtaining the time for -3600 (e.g. strtotime(-3600)) which makes no sense because strtotime expects a string as first argument.

如果您调用strtotime($date - 3600),就像获取 -3600(例如strtotime(-3600))的时间一样,这是没有意义的,因为 strtotime 需要一个字符串作为第一个参数。

回答by Sudhir Bastakoti

You could do:

你可以这样做:


$testDateStr = strtotime($date);
$finalDate = date("Y-m-d H:i:s", strtotime("-1 hour", $testDateStr));

Hope it helps

希望能帮助到你

回答by ghoti

86400 seconds is one DAY, not one hour.

86400 秒是一天,而不是一小时。

The output of strtotime() is a unix timestamp. 3600 seconds earlier is that timestamp minus one hour. So:

strtotime() 的输出是一个 unix 时间戳。早 3600 秒是该时间戳减去一小时。所以:

$date = strtotime($somestring);

$hourago = $date - 3600;

Or, depending on the original format of $somestring:

或者,取决于 $somestring 的原始格式:

$hourago = strtotime($somestring . " - 1 hour");

And don't forget about date_default_timezone_set()

并且不要忘记date_default_timezone_set()

回答by mickmackusa

Nobody offered a DateTimeobject solution yet, so I will. It is the most modern method for handling datetime and doesn't require any calculation of minutes & seconds.

还没有人提供DateTime对象解决方案,所以我会。它是处理日期时间的最现代方法,不需要任何分钟和秒的计算。

I always like to drop in some sort of timezone declaration to make things definitive. Daylight Saving Time is normally a concern with datetime manipulations, but probably okay in this case. Feel free to modify the timezone for your case.

我总是喜欢加入某种时区声明来使事情变得明确。夏令时通常是日期时间操作的问题,但在这种情况下可能没问题。随意修改您的案例的时区。

PHP manual links:

PHP手册链接:

Code (including formatted time for comparison) Demo Link:

代码(包括用于比较的格式化时间)演示链接

$now=new DateTime('NOW UTC');

$now_stamp=$now->getTimestamp();
$formatted_now=$now->format("Y-m-d H:i:s");
echo "$now_stamp ($formatted_now)\n";

$hour_ago=$now->modify('-1 hour');
$hour_ago_stamp=$hour_ago->getTimestamp();
$formatted_hour_ago=$hour_ago->format("Y-m-d H:i:s");
echo "$hour_ago_stamp ($formatted_hour_ago)";

Output:

输出:

1492687193 (2017-04-20 11:19:53)
1492683593 (2017-04-20 10:19:53)

回答by Naveen Kumar

Try this query

试试这个查询

$query= "insert into yourTable(otherValues, date) 
values (otherFields, DATE_ADD(NOW(),INTERVAL -1 hour))

Use DATE_ADD to insert a date value which is 1 hour less than current time.

使用 DATE_ADD 插入比当前时间少 1 小时的日期值。