tags:

views:

220

answers:

5

Hello,

What is the simplest way to do it in PHP ?

I want the date of the Monday of a given week number of a year (example : week number 3 of 2009)

Thanks !

EDIT : If you use Linux only machines, use cletus' solution, however I am looking for something that can work on Windows AND Linux.

+1  A: 

You can use strptime() to get the time.

$time = strptime('1 23 2009', '%w %U %Y');

This will get the time for the Monday (day 1, 0 is Sunday, 6 is Saturday) of the 23rd week of 2009. If you want to format this into a date, use date().

$date = date('d F Y', $time);
cletus
+1: Your way is much easier than mine. :D
NawaMan
Wow, I wish this could also work on Windows machine. I think strptime is not available on Windows machine...
Amadeus45
Yea, says in the notes of the PHP.net page: no-worky on Windows.
seanmonstar
In PHP >= 5.3, it seems the DateTime object contains this functionality, on windows as well. http://www.php.net/manual/en/datetime.createfromformat.php
gnud
A: 

date('N', $date) gives you the day of the week (1 = Monday, 2 = Tuesday, etc).* Just subtract that from your date and you have the Monday.

$date = strtotime('01-01-2009 +3 weeks');
$date = strtotime('-' . (date('N', $date) - 1) . ' days', $date);  // reset to Monday

Depending on what format your date is in you may want to use mktime and some more manual number juggling instead of strtotime, but the general idea is to subtract the "day of the week" value from the current date.

*) If you're using PHP < 5.1 use the Sunday based 'w' option instead.

deceze
Must be based on the week number, not a date...
Amadeus45
Changed the creation of the timestamp. I was just assuming you'd start with *some* timestamp and used a very verbose example to demonstrate the Wed -> Mon change.
deceze
A: 

Here is very simple solution, passing a week no and returns the date.

The ISO8601 standard states that week 1 always fall on the week where Jan 4 falls.

For example, to get a day in the 4th week of the year:

$day_in_week = strtotime("2006-01-04 + 4 weeks"));

Then you can adjust this value to Sunday (as a starting place you can guarantee that you can find):

// Find that day's day of the week (value of 0-6)
$wday = date('w', $day_in_week);
$offset = 6 - $wday; // How far it is from Sunday.
$sunday_in_week = $day_in_week - ($offset * (60 * 60 * 24)); // $offset * seconds in a day

Then, you add the seconds in a day again to get Monday.

$monday_in_week = $sunday_in_week + (60 * 60 * 24);

Note: This method can occasionally have some problems with daylight savings time. A similar, and slightly safer between DST time changes, method would use the DateTime class. However, DateTime is only support in PHP 5.2.0 or later. The method above works in earlier version as well.

Jason
+3  A: 

Yet another solution:

<?php
        $week = 3;
        $year = 2009;

        $timestamp = mktime( 0, 0, 0, 1, 1,  $year ) + ( $week * 7 * 24 * 60 * 60 );
        $timestamp_for_monday = $timestamp - 86400 * ( date( 'N', $timestamp ) - 1 );
        $date_for_monday = date( 'Y-m-d', $timestamp_for_monday );
?>
Alan Haggai Alavi
This is the simplest working solution to date on both my Linux and Windows platforms.
Amadeus45
`date( 'U', $timestamp )` could be shortened to `$timestamp`.
deceze
Updated as per deceze's suggestion.
Alan Haggai Alavi
A: 

Try this function

function MondayOfWeek($WeekNumber, $Year=-1) {
    if ($Year == -1) $Year   = 0+date("Y");
    $NewYearDate             = mktime(0,0,0,1,1,$Year);
    $FirstMondayDate         = 7 + 1 - date("w", mktime(0,0,0,1,1,2009));
    $Dates_fromFirstMonday   = 7 * $WeekNumber;
    $Second_fromFirstMonday  = 60*60*24*($FirstMondayDate + $Dates_fromFirstMonday);
    $MondayDay_ofWeek        = $NewYearDate + $Second_fromFirstMonday;
    $Date_ofMondayDay_ofWeek = 0+date("j", $MondayDay_ofWeek);
    return $Date_ofMondayDay_ofWeek;
}

for($i = 0; $i 

When run, I got:

-5-12-19-26-2-9-16-23-2-9-16-23-30-6-13-20-27-4-11-18-25-1-8-15-22-29-6-13-20-27-3-10-17-24-31-7-14-21-28-5-12-19-26-2-9-16-23-30-7-14-21-28

Hope this helps.

NawaMan