tags:

views:

45

answers:

2

I have two pieces of information extracted from a MySQL database, the year(2009, 2010, ect) and the week (1-52). And I need to convert it in to a date start and date end..

For example:

Year=2010, Week=1 would be (Friday, Jan 1st, 2010) - (Sunday, Jan 3rd, 2010)
Year=2010, Week=33 would be (Monday, Aug 16th, 2010) - (Sunday, Aug 22nd, 2010)
Year=2010, Week=34 would be (Monday, Aug 23rd, 2010) - (Sunday, Aug 29th, 2010)

How would I go about doing that in php ?

A: 

Try this out:

$year = 2000;
$week = 1;
$start = date("l, M jS, Y", strtotime("01 Jan ".$year." 00:00:00 GMT + ".$week." weeks"));
$end = date("l, M jS, Y", strtotime($start." + 1 week"));
echo $start." to ".$end;

You need to set $year and $week. It will then print the interval as specified.

For instance, the output as-is is:

Friday, Jan 7th, 2000 to Friday, Jan 14th, 2000

Note that weeks are indexed from 0-51 (easy to fix).

It's kind of ugly, but it works. Hope that helps!

hb2pencil
+1  A: 
$year = "2010"; // Year 2010
$week = "01"; // Week 1

$date1 = date( "l, M jS, Y", strtotime($year."W".$week."1") ); // First day of week
$date2 = date( "l, M jS, Y", strtotime($year."W".$week."7") ); // Last day of week
echo $date1 . " - " . $date2;

If week number is under 10 then append a 0 before number. 1 won't work, it should be 01.

NAVEED