php 添加 'x' 小时数

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

Add 'x' number of hours to date

phpdatedatetimedateaddhour

提问by Jonah Katz

I currently have php returning the current date/time like so:

我目前有 php 返回当前日期/时间,如下所示:

$now = date("Y-m-d H:m:s");

What I'd like to do is have a new variable $new_timeequal $now + $hours, where $hoursis a number of hours ranging from 24 to 800.

我想做的是有一个新的变量$new_timeequal $now + $hours,其中$hours的小时数从 24 到 800 不等。

Any suggestions?

有什么建议?

回答by fdomig

You may use something like the strtotime()function to add something to the current timestamp. $new_time = date("Y-m-d H:i:s", strtotime('+5 hours')).

您可以使用类似strtotime()函数的东西向当前时间戳添加一些东西。$new_time = date("Y-m-d H:i:s", strtotime('+5 hours')).

If you need variables in the function, you must use double quotes then like strtotime("+{$hours} hours"), however better you use strtotime(sprintf("+%d hours", $hours))then.

如果您需要函数中的变量,则必须使用双引号,然后像strtotime("+{$hours} hours"),但最好使用strtotime(sprintf("+%d hours", $hours))

回答by FAjir

An other solution (object-oriented) is to use DateTime::add

另一种解决方案(面向对象)是使用 DateTime::add

Example:

例子:

$now = new DateTime(); //current date/time
$now->add(new DateInterval("PT{$hours}H"));
$new_time = $now->format('Y-m-d H:i:s');

PHP documentation.

PHP 文档

回答by Zar

You can use strtotime()to achieve this:

您可以使用 strtotime()来实现这一点:

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

回答by Rodrigo Prazim

Correct

正确的

You can use strtotime() to achieve this:

您可以使用 strtotime() 来实现这一点:

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

回答by Ankit

You can also use the unix style time to calculate:

你也可以使用unix风格的时间来计算:

$newtime = time() + ($hours * 60 * 60); // hours; 60 mins; 60secs
echo 'Now:       '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $newtime) ."\n";

回答by vr_driver

Um... your minutes should be corrected... 'i' is for minutes. Not months. :) (I had the same problem for something too.

嗯......你的分钟应该更正......'i'是分钟。不是几个月。:)(我也有同样的问题。

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

回答by Piotr Olaszewski

You can try lib Ouzo goodies, and do this in fluent way:

您可以尝试 lib Ouzo goodies,并以流畅的方式执行此操作:

echo Clock::now()->plusHours($hours)->format("Y-m-d H:m:s");

API's allow multiple operations.

API 允许多个操作。

回答by Muhammad Wasim Akram

I use this , its working cool.

我用这个,它的工作很酷。

//set timezone
date_default_timezone_set('GMT');

//set an date and time to work with
$start = '2014-06-01 14:00:00';

//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));

回答by Vadim Samokhin

I like those built-in php date expressions like +1 hour, but for some reason, they fall out of my head all of the time. Besides, none of the IDEs I'm aware of suggest auto-completion facility for that kind of stuff. And, finally, although juggling with those strtotimeand datefunctions is no rocket science, I have to google their usage each time I need them.

我喜欢那些内置的 php 日期表达式,例如+1 hour,但出于某种原因,它们总是从我的脑海中消失。此外,我所知道的 IDE 中没有一个建议为此类内容提供自动完成功能。最后,虽然处理这些strtotimedate功能不是火箭科学,但每次我需要它们时,我都必须在谷歌上搜索它们的用法。

That's why I like the solution that eliminates (at least mitigates) those issues. Here's how adding xhours to a date can look like:

这就是为什么我喜欢消除(至少减轻)这些问题的解决方案。以下是向x日期添加小时的方式:

(new Future(
    new DateTimeFromISO8601String('2014-11-21T06:04:31.321987+00:00'),
    new NHours($x)
))
    ->value();

As a nice bonus, you don't have to worry about formatting the resulting value, it's already is ISO8601 format.

作为一个不错的奖励,您不必担心格式化结果值,它已经是 ISO8601 格式。

This example uses meringue library, you can check out more examples here.

此示例使用蛋白酥皮库,您可以在此处查看更多示例。

回答by user7282

$date_to_be-added="2018-04-11 10:04:46";
$added_date=date("Y-m-d H:i:s",strtotime('+24 hours', strtotime($date_to_be)));

A combination of date()and strtotime()functions will do the trick.

的组合日期()的strtotime()函数就可以了。