PHP - Strtotime - 添加小时

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

PHP - Strtotime - Add hours

phpstrtotime

提问by TheLettuceMaster

I have this variable:

我有这个变量:

$timestamp = strftime("%Y-%m-%d %h:%M:%S %a", time ());

I simply want to add three hours and echo it out.

我只是想增加三个小时并呼应它。

I have seen the way where you can do the 60 * 60 * 3 method or the hard code "+ 3 hours" where it understands the words.

我已经看到您可以执行 60 * 60 * 3 方法的方式或它理解单词的硬编码“+ 3 小时”。

What is the best way of getting this result?

获得此结果的最佳方法是什么?

采纳答案by Daniil Ryzhkov

$timestamp = strftime("%Y-%m-%d %h:%M:%S %a", time() + 3*60*60)

3*60*60is the best way

3*60*60是最好的方法

回答by Ja?ck

The best way is what you think is more readable. The following expressions are identical:

最好的方法是您认为更具可读性的内容。以下表达式是相同的:

time() + 3 * 60 * 60

strtotime('+3 hours')

回答by s3polz

i always do like this

我总是这样

$current_time = date('Y-m-d H:i:s');
$new_time = strtotime($current_time . "+3hours");
echo $new_time;

or

或者

$new_time = mktime(date('H')+3, 0, 0, date('m'), date('d'), date('Y'));
$new_time = date('Y-m-d H:i:s', $new_time);
echo $new_time;

回答by iWantSimpleLife

$time = new DateTime("+ 3 hour");
$timestamp = $time->format('Y-M-d h:i:s a');

Clear and concise :)

简洁明了:)

回答by Akhil Gupta

Just add seconds to add hours:

只需添加秒即可添加小时:

strtotime($your_date)+2*60*60 

This will add two hours in your date.

这将在您的约会中增加两个小时。

回答by sachleen

You can use DateTime::modifyto add time, but I would just do time()+10800.

你可以DateTime::modify用来增加时间,但我只会做time()+10800

回答by Marc B

If you want to go 'modern':

如果你想“现代”:

$d = new DateTime();
$d->add(new DateInterVal('P3H'));
$timestamp = $d->format('Y-M-d h:i:s a');

refs: DateTimeobject

参考:日期时间对象