Php 如何从一年中的某一天转到日期,反之亦然

2022-08-30 23:08:43

你如何从一年中的第n天到日期进入php,例如:

getdatefromday(275, 2012) 

并且它输出一个日期(如果是对象,则更好)。

我也想做相反的事情,比如getdayoftheyear("21 oct 2012")


答案 1

这很容易。您应该在此处阅读对象的静态方法,在此处阅读函数,在此处阅读函数。DateTimecreateFromFormatdatestrtotime

// This should get you a DateTime object from the date and year.
function getDateFromDay($year, $dayOfYear) {
  $date = DateTime::createFromFormat('z Y', strval($year) . ' ' . strval($dayOfYear));
  return $date;
}

// This should get you the day of the year and the year in a string.
date('z Y', strtotime('21 oct 2012'));

答案 2

尝试(天数从非开始):01

$date = DateTime::createFromFormat( 'Y z' , '2012 275');
var_dump($date);

并且:

echo date('z', strtotime('21 oct 2012'));

推荐