[Tfug] PHP calculating date from week

Ammon Lauritzen ammon at simud.org
Tue Jun 20 14:59:53 MST 2006


Adrian wrote:
> Anybody have a simple week to date function in PHP worked out? I have been 
> banging my head trying to figure out something simple... without 20 lines of 
> recursive logic to handle corner cases...

Ok, let's see if I can think long enough to write this out for you :P

$weekstamp = mktime(0,0,0,1,$week*7,$year);

This gives a unix timestamp for midnight on the week x7th day of the 
year and should probably save you a few cycles wasted making two 
strtotime() calls.

I'd then use localtime($weekstamp)["tm_wday"] to get the day of the week 
in stead of date("w"), it's probably a touch faster.

Ammon




More information about the tfug mailing list