PHP - Strtotime - Add hours

谁说我不能喝 提交于 2019-12-03 12:28:31
$timestamp = strftime("%Y-%m-%d %h:%M:%S %a", time() + 3*60*60)

3*60*60 is the best way

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

time() + 3 * 60 * 60

strtotime('+3 hours')

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

Clear and concise :)

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

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

Just add seconds to add hours:

strtotime($your_date)+2*60*60 

This will add two hours in your date.

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