我有值
$days = 166.0444;
这是一年中的一天。我如何简单地将这个值转换为它的日期时间,它应该是
2013-06-15 01:04:ss
当然,我可以减去一个月中的天数来得到一个月,然后继续计算天数等等,但还有其他方法吗?
编辑:年份总是当前年份
谢谢大家,我就是这样做的:
$dayOfYear = (int)$result;
$temp = ($result - $dayOfYear) * 24;
$hours = (int)$temp;
$temp = ($temp - $hours) * 60;
$minutes = (int)$temp;
$temp = ($temp - $minutes) * 60;
$seconds = (int)$temp;
$date = new DateTime();
$date->add(new DateInterval('P' . $dayOfYear . 'D'));
$date->add(new DateInterval('PT' . $hours . 'H'));
$date->add(new DateInterval('PT' . $minutes . 'M'));
$date->add(new DateInterval('PT' . $seconds . 'S'));
如果是当前年份,您可以
$days = 166.0444;
$date = new DateTime();
$date->setDate($date->format('Y'), 1, 1);
$date->add(new DateInterval('P' . floor($days) . 'D'));
你可以用这种或类似的方式:
function convertDaysToDateTime($days, $year){
$datetime = new DateTime();
return $datetime->setTimestamp(mktime(0,0,0,0,0,$year) + $days * 24 * 3600);
}
$days = 166.0444;
$datetime = convertDaysToDateTime($days, date('Y'));
echo $datetime->format('U = Y-m-d H:i:s');