Page 1 of 1

Get the date of this year from the day of year

Posted: Mon Sep 04, 2006 11:49 am
by joachimseitz
I have the day of this year. (1-365)
I want to get the date it would be this year! (2006) (obviously its always same except on leap years)
Like 04.09

What would be the fastest way to do that?

I can easily get the day of the year through date("z"); but how can I get the day of motnh and month from day of year?

Couldn't think of anything yet except like make if day=1 and year 2006 then date is 01.01.2006, so basicly doing that for all days would be alot. I'm pretty sure there should be a solution to it :)

Posted: Mon Sep 04, 2006 11:51 am
by RobertGonzalez
date() or mktime(), perhaps?

Posted: Mon Sep 04, 2006 1:12 pm
by joachimseitz
Hmm

well to use mktime you need the day of month and the month, which I dont have yet
and date(); only allows me get the day of the year but not viceversa, getting the day of month and month from the day of the year

or? I could be missing something ...

Posted: Mon Sep 04, 2006 1:18 pm
by Todd_Z
Untested, just for theory purposes:

Code: Select all

$year = 2006;
$day = 194;
$ts = mktime( 12, 0, 0, 1, 1, $year );
$ts += 86400 * ( $day - 1 );
date( 'r', $ts );

Posted: Mon Sep 04, 2006 1:31 pm
by joachimseitz
good idea!

ill test ;)

Posted: Mon Sep 04, 2006 1:33 pm
by volka
even shorter

Code: Select all

<?php
$year = 2006;
$day = 194;

$ts = mktime( 12, 0, 0, 1, $day, $year );
echo date( 'r', $ts );
?>

Posted: Mon Sep 04, 2006 1:38 pm
by Todd_Z
Didn't realize that that would work, that's kind of a hack, but whatever, it works! :)

Posted: Mon Sep 04, 2006 1:40 pm
by volka
not a hack but an assured feature
http://de2.php.net/mktime wrote:mktime() is useful for doing date arithmetic and validation, as it will automatically calculate the correct value for out-of-range input.

Posted: Mon Sep 04, 2006 1:40 pm
by Todd_Z
i stand corrected then. :oops:

Posted: Mon Sep 04, 2006 2:09 pm
by joachimseitz
Yes it does work :)
Thanx