note 60796 added to function.date
| From: | infolockatgmaildotcom at osu1 dot php dot net | Date: | Mon, 16 Jan 2006 22:39:01 +0000 |
| Subject: | note 60796 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101944@lists.php.net to get a copy of this message | ||
cml at dokuro dot org made a nice function but it wasn't working for the ones of us who have
sunday the first day of the week. Also, I needed a version of his function that would get the
actual week of operation for the month in itself, and also only that year. His function would grab
the beginning of the week regardless (ie, 2008 would return the sunday of 2007 instead of the monday
of 2008 and then go from there).
So, I rewrote this to handle these things. For example, if the first day of the year is on a tuesday
it will create the first key to be january 1, and the next key to be january 6. Otherwise,
it's work as usual.
<?php
function get_weeks($year) {
$firstday_ts = mktime(1,0,0,1,1,$year);
$firstday = date("w", $firstday_ts);
$weeks[0] = date('Y-m-d',$firstday_ts);
$first = $firstday_ts;
if($firstday != 0) {
$x = 8 - $firstday;
$firstday_ts = mktime(1,0,0,1,$x,$year);
$first = $firstday_ts;
$weeks[] = date("Y-m-d", $first);
}
$first = strtotime("+7 day",$first);
for($i=0; $year == date("Y", $first); $i++, $first = strtotime("+7
day",$first)) {
$weeks[] = date("Y-m-d", $first);
}
return $weeks;
}
?>
Result :
Array
(
[0] => 2006-01-01
[1] => 2006-01-08
[2] => 2006-01-15
[3] => 2006-01-22
[4] => 2006-01-29
[5] => 2006-02-05
[6] => 2006-02-12
[7] => 2006-02-19
[8] => 2006-02-26
[9] => 2006-03-05
[10] => 2006-03-12
[11] => 2006-03-19
[12] => 2006-03-26
[13] => 2006-04-02
[14] => 2006-04-09
[15] => 2006-04-16
[16] => 2006-04-23
[17] => 2006-04-30
[18] => 2006-05-07
[19] => 2006-05-14
[20] => 2006-05-21
[21] => 2006-05-28
[22] => 2006-06-04
[23] => 2006-06-11
[24] => 2006-06-18
[25] => 2006-06-25
[26] => 2006-07-02
[27] => 2006-07-09
[28] => 2006-07-16
[29] => 2006-07-23
[30] => 2006-07-30
[31] => 2006-08-06
[32] => 2006-08-13
[33] => 2006-08-20
[34] => 2006-08-27
[35] => 2006-09-03
[36] => 2006-09-10
[37] => 2006-09-17
[38] => 2006-09-24
[39] => 2006-10-01
[40] => 2006-10-08
[41] => 2006-10-15
[42] => 2006-10-22
[43] => 2006-10-29
[44] => 2006-11-05
[45] => 2006-11-12
[46] => 2006-11-19
[47] => 2006-11-26
[48] => 2006-12-03
[49] => 2006-12-10
[50] => 2006-12-17
[51] => 2006-12-24
[52] => 2006-12-31
)
<?php
md5('Jonathon Hibbard, Clear Channel Communications');
?>
----
Server IP: 12.160.245.96Probable Submitter: 69.61.136.254
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60796
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60796&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60796&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60796&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60796&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60796&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60796&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60796&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60796&report=yes
Search -- http://master.php.net/manage/user-notes.php