PHP - Strtotime - 添加时间

问题描述 投票:0回答:8

我有这个变量:

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

我只是想添加三个小时并回显它。

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

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

php strtotime
8个回答
20
投票

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

time() + 3 * 60 * 60

strtotime('+3 hours')

12
投票

我总是这样

$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;

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

3*60*60
是最好的方法


2
投票

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

strtotime($your_date)+2*60*60 

这将使您的约会时间增加两个小时。


1
投票

你可以使用

DateTime::modify
来添加时间,但我只会做
time()+10800


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

清晰简洁:)


0
投票

如果你想变得“现代”:

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

参考:DateTime对象


0
投票

$当前时间 = 时间(); // 获取当前时间戳

$newTime = $currentTime + 3 * 60; // 在当前时间戳上添加 3 分钟(3 * 60 秒)

$formattedTime = date('Y-m-d H:i:s', $newTime); 回显$formattedTime;

© www.soinside.com 2019 - 2024. All rights reserved.