簡體   English   中英

PHP的strtotime +2.5小時的錯誤?

[英]php strtotime +2.5 hours bug?

strtotime有一些意外的行為:

$d = '2.5';                                   // i.e. hours
$t = '2014-03-20 08:30:00';                   // mysql datetime
$ut = strtotime($t);                          // value: 1395297000
$s = $d * 60 * 60;                            // duration in seconds: 9000
$plusHrs = strtotime($t.' +'.$d.' hours');    // value: 1395315000
$plusSec = strtotime($t.' +'.$s.' seconds');  // value: 1395306000

預期的行為是$ plusHrs = $ plusSec。 無論輸入什么日期,都會發生這種情況,這意味着它不是夏令時。

這是錯誤嗎? (PHP v5.3.14)

不,這不是錯誤。 strtotime()需要相對格式的整數值。 因此,您的+2.5小時將被視為"+2 GMT" ,然后 "+5"小時和點將被忽略。 您可以將其更改為逗號,甚至也可以刪除-結果不會更改,因為同樣,僅會解析整數。 因此,此類聲明將被視為相對於GMT偏移的相對小時數:

//this is the same things:
//and they mean set timesone to GMT+2 & add 5 hours:
date('Y-m-d H:i:s', strtotime('now +2.5 hours'));
date('Y-m-d H:i:s', strtotime('now +2,5 hours'));
date('Y-m-d H:i:s', strtotime('now +2 5 hours'));

因此,例如, +1.5 hoursadd 5 hours for time in GMT +1 這意味着,最終結果將取決於當前時區,因為初始時間戳將設置為當前時區( now在上面的示例中)。

$plusHrs = strtotime($t.' +'.$d.' hours');    
$plusSec = strtotime($t.' +'.$s.' seconds'); 

These two are different, $s = $d * 60 * 60 while $d = '2.5.

您可以嘗試使用2.5小時

$plusHrs = strtotime($t.' + 2 hours 30 minutes'); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM