Php how to go from day of the year to date and vice versa

This is pretty easy all around. You should read up on the DateTime object's createFromFormat static method here, the date function here and the strtotime function here.

// 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'));

Try(days starts from 0 not 1):

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

and that:

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

Tags:

Php

Date