note 95919 modified in function.date by danbrown
| From: | danbrown@php.net | Date: | Thu, 28 Jan 2010 18:50:42 +0000 |
| Subject: | note 95919 modified in function.date by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165464@lists.php.net to get a copy of this message | ||
I suppose we run an off week in which week one is considered to start on the Monday of the week that
contains Jan 1. All the date functions seemed to be incorrect for this week calculation so
here's what I came up with to give me the week number we needed. Ideally we aren't the
only people in the world that uses that calculation:
So:
week_of_year("12", "27", "2009") is "52"
week_of_year("12", "28", "2009") is "1"
week_of_year("1", "4", "2010") is "2"
Id assume if you needed to start on another weekday you could modify it to look for that day instead
of Mon.
<?php
function week_of_year($month, $day, $year) {
//Get date supplied Timestamp;
$thisdate = mktime(0,0,0,$month,$day,$year);
//If the 1st day of year is a monday then Day 1 is Jan 1
if (date("D", mktime(0,0,0,1,1,$year)) == "Mon"){
$day1=mktime (0,0,0,1,1,$year);
} else {
//If date supplied is in last 4 days of last year then find the monday before Jan 1 of next
year
if (date("z", mktime(0,0,0,$month,$day,$year)) >= "361"){
$day1=strtotime("last Monday", mktime(0,0,0,1,1,$year+1));
} else {
$day1=strtotime("last Monday", mktime(0,0,0,1,1,$year));
}
}
// Calcualte how many days have passed since Day 1
$dayspassed=(($thisdate - $day1)/60/60/24);
//If Day is Sunday then count that day other wise look for the next sunday
if (date("D", mktime(0,0,0,$month,$day,$year))=="Sun"){
$sunday = mktime(0,0,0,$month,$day,$year);
} else {
$sunday = strtotime("next Sunday", mktime(0,0,0,$month,$day,$year));
}
// Calculate how many more days until Sunday from date supplied
$daysleft = (($sunday - $thisdate)/60/60/24);
// Add how many days have passed since figured Day 1
// plus how many days are left in the week until Sunday
// plus 1 for today
// and divide by 7 to get what week number it is
$thisweek = ($dayspassed + $daysleft+1)/7;
return $thisweek;
}
?>
--was--
I suppose we run an off week in which week one is considered to start on the Monday of the week that
contains Jan 1. All the date functions seemed to be incorrect for this week calculation so
here's what I came up with to give me the week number we needed. Ideally we aren't the
only people in the world that uses that calculation:
So:
week_of_year("12", "27", "2009") is "52"
week_of_year("12", "28", "2009") is "1"
week_of_year("1", "4", "2010") is "2"
Id assume if you needed to start on another weekday you could modify it to look for that day instead
of Mon.
function week_of_year($month, $day, $year) {
//Get date supplied Timestamp;
$thisdate = mktime(0,0,0,$month,$day,$year);
//If the 1st day of year is a monday then Day 1 is Jan 1
if (date("D", mktime(0,0,0,1,1,$year)) == "Mon"){
$day1=mktime (0,0,0,1,1,$year);
} else {
//If date supplied is in last 4 days of last year then find the monday before Jan 1 of next
year
if (date("z", mktime(0,0,0,$month,$day,$year)) >= "361"){
$day1=strtotime("last Monday", mktime(0,0,0,1,1,$year+1));
} else {
$day1=strtotime("last Monday", mktime(0,0,0,1,1,$year));
}
}
// Calcualte how many days have passed since Day 1
$dayspassed=(($thisdate - $day1)/60/60/24);
//If Day is Sunday then count that day other wise look for the next sunday
if (date("D", mktime(0,0,0,$month,$day,$year))=="Sun"){
$sunday = mktime(0,0,0,$month,$day,$year);
} else {
$sunday = strtotime("next Sunday", mktime(0,0,0,$month,$day,$year));
}
// Calculate how many more days until Sunday from date supplied
$daysleft = (($sunday - $thisdate)/60/60/24);
// Add how many days have passed since figured Day 1
// plus how many days are left in the week until Sunday
// plus 1 for today
// and divide by 7 to get what week number it is
$thisweek = ($dayspassed + $daysleft+1)/7;
return $thisweek;
}
http://php.net/manual/en/function.date.php