我有这个变量:
$timestamp = strftime("%Y-%m-%d %h:%M:%S %a", time ());
我只是想添加三个小时并回显它。
我已经看到了可以执行 60 * 60 * 3 方法或硬编码“+ 3 小时”的方法,它可以理解这些单词。
获得此结果的最佳方法是什么?
最好的方法是你认为更具可读性的方法。以下表达式是相同的:
time() + 3 * 60 * 60
strtotime('+3 hours')
我总是这样
$current_time = date('Y-m-d H:i:s');
$new_time = strtotime($current_time . "+3hours");
echo $new_time;
或
$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;
$timestamp = strftime("%Y-%m-%d %h:%M:%S %a", time() + 3*60*60)
3*60*60
是最好的方法
只需添加秒即可添加小时:
strtotime($your_date)+2*60*60
这将使您的约会时间增加两个小时。
$time = new DateTime("+ 3 hour");
$timestamp = $time->format('Y-M-d h:i:s a');
清晰简洁:)
如果你想变得“现代”:
$d = new DateTime();
$d->add(new DateInterVal('P3H'));
$timestamp = $d->format('Y-M-d h:i:s a');
参考:DateTime对象
$当前时间 = 时间(); // 获取当前时间戳
$newTime = $currentTime + 3 * 60; // 在当前时间戳上添加 3 分钟(3 * 60 秒)
$formattedTime = date('Y-m-d H:i:s', $newTime); 回显$formattedTime;