note 65515 added to function.date
| From: | johndotlobaughatgmaildotcom at osu1 dot php dot net | Date: | Tue, 02 May 2006 16:05:34 +0000 |
| Subject: | note 65515 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-110373@lists.php.net to get a copy of this message | ||
<?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
----
Server IP: 64.71.164.2
Probable Submitter: 12.110.112.57
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/note/edit/65515
Del: integrated -- http://master.php.net/note/delete/65515/integrated
Del: useless -- http://master.php.net/note/delete/65515/useless
Del: bad code -- http://master.php.net/note/delete/65515/bad+code
Del: spam -- http://master.php.net/note/delete/65515/spam
Del: non-english -- http://master.php.net/note/delete/65515/non-english
Del: in docs -- http://master.php.net/note/delete/65515/in+docs
Del: other reasons-- http://master.php.net/note/delete/65515
Reject -- http://master.php.net/note/reject/65515
Search -- http://master.php.net/manage/user-notes.php