note 65515 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 11 Jun 2006 11:46:31 +0000 |
| Subject: | note 65515 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-114267@lists.php.net to get a copy of this message | ||
Note Submitter: john dot lobaugh at gmail dot com
----
<?php
/*This uses the PHP date function date("W") that retuns the current week of the year (1 to
52) and calculates and retuns the Monday through Sunday dates of a given week, where the week begins
on Monday*/
function call_week($yearweek){ //Uses the defined format "yyyy/ww" to pull Monday to
Sunday dates for given week
list($callyear, $callweek) = split('[/]', $yearweek); //Get's the user defined year
and week
$thisday = mktime(0,0,0,date("m"),date("d"),date("Y")); //Set today
at 00:00 as starting point
$thisweek = date("W", $thisday); //Gets current week in year (1 to 52)
$thisyear = date("Y", $thisday); //Gets current year in yyyy format
$weekday = date("w", $thisday); //Gets current day of week, i.e. 0 = Sunday, 1 =
Monday...
$year_diff = $thisyear - $callyear; //Calculates difference between current year and call year
$week_diff = $thisweek - $callweek; //Calculates difference between current year and call week
$week_start = ($weekday - 1); //Calculates difference between today and Monday
$week_end = (7 - $weekday); //Calculates difference between today and Sunday
if($callyear != $thisyear){ //If week is not in current year
$first_day = ((7 * $week_diff) + $week_start) - $year_diff; //Finds number of days to subtract for
given week and adjusts for year
$last_day = (((7 * $week_diff) - $week_end) * -1) + $year_diff; //Find number of days to subtract
for given week and adjusts for year
}
else{ //If week is in current year
$first_day = (7 * $week_diff) + $week_start; //Finds number of days to subtract for given week
$last_day = ((7 * $week_diff) - $week_end) * -1; //Finds number of days to subtract for given week
}
$monday = mktime(0,0,0,date("m"),date("d") - $first_day,date("Y") -
$year_diff); //Adjusts to Monday of given week from today
$sunday = mktime(0,0,0,date("m"),date("d") + $last_day,date("Y") -
$year_diff); //Adjusts to Sunday of given week from today
//Use the PHP date function variables to alter the format of the date output
$monday_date = date("l, F j, Y", $monday); //Sets Monday's date for given week
$sunday_date = date("l, F j, Y", $sunday); //Sets Sunday's date for given week
return $monday_date . " to " . $sunday_date; //Returns the date span for the given week
}
//It can be used something like this
echo "The 17th week of 2006 is<br />\n";
echo call_week('2006/17') . "<br />\n";
?>
Returns:
The 17th week of 2006 is
Monday, April 24, 2006 to Sunday, April 30, 2006