Calculate Date from day of year in PhP

So why would you want to calculate a day of year to a date ?
Well I needed it for my Sudoku script. (
I wanted to show a new Sudoku every day, and let the visitors be able to show the sudoku of yesterday and the days before.

To get a “random” daily new sudoku I use the year day number. (so you’ve got 365 year days a year)
When going to the yesterday’s sudoku puzzle I do a day of year – 1.
But on the same screen I like to show the date.
So here you are…. I want to be able to calculate the date from the day of year I have choosen.
Here’s that code

$todayid = date(“z”); // to get today’s day of year

function dayofyear2date( $tDay, $tFormat = ‘d-m-Y’ ) {
$day = intval( $tDay );
$day = ( $day == 0 ) ? $day : $day – 1;
$offset = intval( intval( $tDay ) * 86400 );
$str = date( $tFormat, strtotime( ‘Jan 1, ‘ . date( ‘Y’ ) ) + $offset );
return( $str );

echo dayofyear2date($todayid);

ps: did you know that 1 year is not exactly 365 days ? 1 year is 365,242199 dagen
ps2: That is why the leap-year exists.

Nerd Herder ☆ Scrum Master ☆ Jira Ninja ☆ Freeletics evangelist ☆ Photo Shooter ☆ Dad ☆ Husband ☆ Zookeeper ☆ Blogger Fit Dutch Bodies ☆ Dutch

Site Footer