note 94851 added to function.date
| From: | andrea at bhweb dot it | Date: | Sat, 28 Nov 2009 15:02:16 +0000 |
| Subject: | note 94851 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163363@lists.php.net to get a copy of this message | ||
I created two functions useful to those that need to work with MySQL's YEARWEEK.
<?php
/**
* Gives a YEARWEEK compatible with MySQL's YEARWEEK('date', 3)
*
* @param int|null $date the timestamp or null = now
* @return string YEARWEEK
* @author Andrea Baron <andrea@bhweb.it>
*/
function yearweek($date = null) {
if($date === null)
$date = time();
return sprintf('%04d%02d', date('o', $date), date('W', $date));
}
/**
* Gives starting day (monday) and ending day (sunday) of the given
* YEARWEEK
*
* @param int|string $yw the YEARWEEK
* @return array on index 0 the start, on index 1 the end
* @author Andrea Baron <andrea@bhweb.it>
*/
function yearweekToLimits($yw) {
$jan1st = mktime(0, 0, 0, 1, 1, floor($yw / 100));
$sw = $yw % 100 - 2;
$ew = $sw + 1;
$dow = idate('w', $jan1st);
if($dow >= 5 || $dow <= 0) {
++$sw;
++$ew;
}
elseif($dow === 1)
++$sw;
if($sw >= 0)
$sw = '+'.$sw;
if($ew >= 0)
$ew = '+'.$ew;
return array(
strtotime($sw.' week mon', $jan1st),
strtotime($ew.' week sun', $jan1st),
);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 89.189.42.206
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/94851
Del: integrated -- https://master.php.net/note/delete/94851/integrated
Del: useless -- https://master.php.net/note/delete/94851/useless
Del: bad code -- https://master.php.net/note/delete/94851/bad+code
Del: spam -- https://master.php.net/note/delete/94851/spam
Del: non-english -- https://master.php.net/note/delete/94851/non-english
Del: in docs -- https://master.php.net/note/delete/94851/in+docs
Del: other reasons-- https://master.php.net/note/delete/94851
Reject -- https://master.php.net/note/reject/94851
Search -- https://master.php.net/manage/user-notes.php