因为这不是真正显示如何使用“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