ZimplicIT

Get week starting and ending date in PHP

This is how you can get the week starting date or ending date by giving a week number and the year.

<?php
function week_start_date($week, $year, $format = 'Ymd') {
   
   
$Jan1 = mktime(0,0,0,1,1,$year);
   
$MondayOffset = (11-date('w',$Jan1))%7-3;
   
$desiredMonday = strtotime(($week-1) . ' weeks '.$MondayOffset.' days', $Jan1);
   
$desiredMonday = date($format, $desiredMonday);
   
    return
$desiredMonday;
}

$sStartDate = week_start_date($week_number, $year);
$sEndDate   = date('Y-m-d', strtotime('+6 days', strtotime($sStartDate)));

?>
Reference : http://www.phpbuilder.com/board/showthread.php?t=10222903

Going from date or timestamp to get the week start and end date this call might be a better way to go.

<?php
$time
= strtotime("2011-01-01");
//or
$time = strtotime("2010-12-31");

$first_day_of_week = date('Y-m-d', strtotime('Last Monday', $time));
$last_day_of_week = date('Y-m-d', strtotime('Next Sunday', $time));

//Will give the same result
//2010-12-27
//2011-01-02
?>

<?php
/**
* Get week and its start and ending date or timestamp
*
* @param unknown_type $date
*/
function week_start_end_by_date($date, $format = 'Ymd') {
   
    if (
is_numeric($date)) {
       
$time = $date;
    }else{
       
$time = strtotime($date);
    }
   
   
$week['week'] = date('W', $time);
   
$week['first_day_of_week'] = date($format, strtotime('Last Monday', $time));
   
$week['last_day_of_week'] = date($format, strtotime('Next Sunday', $time));
   
    return
$week;
}
?>