PHP添加到时间


php add to time

为什么$now 2不起作用?

$now = date('Y-m-d H:i:s', time()); 
$now2 = date("Y-m-d H:i:s", strtotime( "$now + 0.5 secs"));

或者我怎样才能让它工作?

它现在工作的原因是因为PHP不识别0.5 secs有效

0.5 secs不是有效的日期格式,但它是有效的microtime

尝试

$now = date('Y-m-d H:i:s', time());
var_dump(strtotime( "$now + 1 secs"));

输出

int 1334188908

time() 返回自纪元以来的数。它对几分之一秒一无所知。如果您需要这种精度级别,则需要使用 microtime()(请参阅:http://php.net/manual/en/function.microtime.php)

编辑:您当然不能在date()格式中使用微时间,因此您需要先进行计算,然后再使用它。似:

$now = microtime(true);
$newtime = $now + 0.5;
echo date("Y-m-d H:i:s", round($newtime,0) );

根据您的要求,您可能更喜欢使用与 round() 不同的函数来使 $newtime 和整数再次适合使用 date() 进行格式化

Unix 时间戳的分辨率(这是time()返回的)仅为 1 秒。所以你不能加半秒。