note 94851 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 15 Aug 2010 18:41:57 +0000 |
| Subject: | note 94851 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-171709@lists.php.net to get a copy of this message | ||
Note Submitter: andrea at bhweb dot it
----
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),
);
}
?>