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. (http://sudoku.gebruikmaar.nl)
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.

Theo van der Sluijs

Theo van der Sluijs (1975) is Software Development Manager at Bax-shop.nl. You can read more in dutch on his “Dutch Blog“, he also created Gebruikmaar.nl. He is a member of the Board of the Dutch Flex Usergroup and Goesweb.net.

Google+ Twitter LinkedIn 



VN:F [1.9.13_1145]
Rating: 7.3/10 (10 votes cast)
VN:F [1.9.13_1145]
Rating: -1 (from 3 votes)
Calculate Date from day of year in PhP, 7.3 out of 10 based on 10 ratings

Related Posts

10 comments

  1. I needed it for a gantt chart I am (attempting) to develop in Flex. Works great. Thanks

    VA:F [1.9.13_1145]
    Rating: 4.5/5 (2 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  2. Thank you ever so much! I was trying to avoid having to recode daily content with a datestamp! This will do the trick nicely without tons of typing. You rock!!

    VA:F [1.9.13_1145]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  3. Christian Sciberras

    Oh, and the right code would be:

    function dayofyear2date( $tDay, $tFormat = 'd-m-Y' ) {

    $offset = intval($tDay) * 86400;

    $date = mktime( 0, 0, 0, 1, 1, date('Y') )+$offset;

    return date( $tFormat, $date );

    }

    $todayid = date("z"); // to get today's day of year

    echo dayofyear2date($todayid);

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
    • Thanks Christian, this new code looks great.

      VN:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
    • Garison

      Actually, the right right code would be:

      function dayofyear2date( $tDay, $tFormat = 'd-m-Y' ) {
      $date = mktime( 0, 0, 0, 1, $tDay, date('Y') );
      return date( $tFormat, $date );
      }

      $todayid = date("z"); // to get today's day of year
      echo dayofyear2date($todayid);

      VA:F [1.9.13_1145]
      Rating: 0.0/5 (0 votes cast)
      VA:F [1.9.13_1145]
      Rating: 0 (from 0 votes)
      • Cesar Perales

        actually the correct function will be

        function dayofyear2date( $tDay, $tFormat = 'd-m-Y' ) {
        $date = mktime( 0, 0, 0, 1, $tDay+1, date('Y') );
        return date( $tFormat, $date );
        }

        $todayid = date("z"); // to get today's day of year
        echo dayofyear2date($todayid);

        Beacuse date('z') starts from 0 and mktime count the number of days from 1

        VA:F [1.9.13_1145]
        Rating: 0.0/5 (0 votes cast)
        VA:F [1.9.13_1145]
        Rating: 0 (from 0 votes)
  4. Süha Kozbey

    Thank you very much, I could'nt find an alternative to your function.. you really saved my time..

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  5. I could have never figured it out (within 2 or 3 weeks!!), thank you!

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: +1 (from 1 vote)
  6. Things are even more simple:

    function DayToTimestamp($day, $year = null) {
    isset($year) or $year = date('Y');
    return strtotime("1 Jan $year +$day day");
    }

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  7. Thx

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes