你如何从一年中的第n天到日期,如:
getdatefromday(275, 2012)
Run Code Online (Sandbox Code Playgroud)
它输出一个日期(如果一个对象更好).
我也想做相反的事情,比如说 getdayoftheyear("21 oct 2012")
Ste*_*ley 20
这很简单.你应该在阅读了DateTime对象的createFromFormat静态方法在这里,该date功能在这里和strtotime功能在这里.
// This should get you a DateTime object from the date and year.
function getDateFromDay($dayOfYear, $year) {
$date = DateTime::createFromFormat('z Y', strval($dayOfYear) . ' ' . strval($year));
return $date;
}
// This should get you the day of the year and the year in a string.
date('z Y', strtotime('21 oct 2012'));
Run Code Online (Sandbox Code Playgroud)
尝试(从几天开始0不1):
$date = DateTime::createFromFormat( 'Y z' , '2012 275');
var_dump($date);
Run Code Online (Sandbox Code Playgroud)
然后:
echo date('z', strtotime('21 oct 2012'));
Run Code Online (Sandbox Code Playgroud)