Get the date of this year from the day of year

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joachimseitz
Forum Commoner
Posts: 25
Joined: Fri Feb 20, 2004 10:36 am
Location: Germany
Contact:

Get the date of this year from the day of year

Post 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 :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

date() or mktime(), perhaps?
joachimseitz
Forum Commoner
Posts: 25
Joined: Fri Feb 20, 2004 10:36 am
Location: Germany
Contact:

Post 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 ...
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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 );
joachimseitz
Forum Commoner
Posts: 25
Joined: Fri Feb 20, 2004 10:36 am
Location: Germany
Contact:

Post by joachimseitz »

good idea!

ill test ;)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

even shorter

Code: Select all

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

$ts = mktime( 12, 0, 0, 1, $day, $year );
echo date( 'r', $ts );
?>
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Didn't realize that that would work, that's kind of a hack, but whatever, it works! :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

i stand corrected then. :oops:
joachimseitz
Forum Commoner
Posts: 25
Joined: Fri Feb 20, 2004 10:36 am
Location: Germany
Contact:

Post by joachimseitz »

Yes it does work :)
Thanx
Post Reply