因爲這不是真正顯示如何使用「z」選項將YYYY:DDD:HH:MM:SS時間轉換爲unix秒,您必須創建自己的函數將DOY轉換爲月份和月份的日期。這是我所做的:
function _IsLeapYear ($Year)
{
$LeapYear = 0;
# Leap years are divisible by 4, but not by 100, unless by 400
if (($Year % 4 == 0) || ($Year % 100 == 0) || ($Year % 400 == 0)) {
$LeapYear = 1;
}
return $LeapYear;
}
function _DaysInMonth ($Year, $Month)
{
$DaysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
return ((_IsLeapYear($Year) && $Month == 2) ? 29 : $DaysInMonth[$Month - 1]);
}
function yydddhhssmmToTime($Year, $DOY, $Hour, $Min, $Sec)
{
$Day = $DOY;
for ($Month = 1; $Day > _DaysInMonth($Year, $Month); $Month++) {
$Day -= _DaysInMonth($Year, $Month);
}
$DayOfMonth = $Day;
return mktime($Hour, $Min, $Sec, $Month, $DayOfMonth, $Year);
}
$timeSec = yydddhhssmmToTime(2016, 365, 23, 23, 23);
$str = date("m/d/Y H:i:s", $timeSec);
echo "unix seconds: " . $timeis . " " . $str ."<br>";
頁面上的輸出顯示其工作,因爲我可以將秒轉回原始輸入值。 unix seconds:1483140203 12/30/2016 23:23:23
這是一個很好的答案。應該真的得到更多的讚揚!感謝 – andreapier 2013-03-15 20:45:33
支持日期時間 'M':'''$ schedule_format = str_replace函數(陣列( 'M', 'Y', 'M', 'd', 'H', 'I', 'A'),陣列( '%b', '%Y', '%M', '%d', '%I', '%M', '%p'),$ DFORMAT);''' – kenorb 2013-09-09 15:59:54
有一個小缺陷,因爲'H'應該被替換爲%H(24小時格式),而不是%I(12小時格式)。所以這裏改進了一行:''schedule_format = str_replace(array('M','Y','m','d','H','i','a'),array('%b 」, '%Y', '%M', '%d', '%H', '%M', '%p'),$ DFORMAT);''' – kenorb 2013-09-19 08:40:25