note 95919 added to function.date
| From: | justinclark at mydesktophelp dot com | Date: | Thu, 28 Jan 2010 18:47:14 +0000 |
| Subject: | note 95919 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165463@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.
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;
}
----
Server IP: 69.147.83.197
Probable Submitter: 64.102.43.116
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/95919
Del: integrated -- https://master.php.net/note/delete/95919/integrated
Del: useless -- https://master.php.net/note/delete/95919/useless
Del: bad code -- https://master.php.net/note/delete/95919/bad+code
Del: spam -- https://master.php.net/note/delete/95919/spam
Del: non-english -- https://master.php.net/note/delete/95919/non-english
Del: in docs -- https://master.php.net/note/delete/95919/in+docs
Del: other reasons-- https://master.php.net/note/delete/95919
Reject -- https://master.php.net/note/reject/95919
Search -- https://master.php.net/manage/user-notes.php