PHP - Strtotime - Add hours

强颜欢笑 提交于 2019-12-21 04:06:28

问题


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.

What is the best way of getting this result?


回答1:


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

3*60*60 is the best way




回答2:


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

time() + 3 * 60 * 60

strtotime('+3 hours')



回答3:


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;



回答4:


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

Clear and concise :)




回答5:


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




回答6:


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: DateTime object




回答7:


Just add seconds to add hours:

strtotime($your_date)+2*60*60 

This will add two hours in your date.



来源:https://stackoverflow.com/questions/11076334/php-strtotime-add-hours

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!