YEARBOOKS between two dates
I would like to have an array of summer days between two dates using MySQL / PHP.
Until now, I have done it simply, since the dates could not be in different years.
// $yw_min = yearweek of the min date
// $yw_max = yearweek of the max date
$yearweeks = array();
for ($i = $yw_min; $i <= $yw_max; $i++) {
$yearweeks[$i] = "W " . substr($i, 4, 6);
}
/*
yearweeks : Array (
[201501] => W 01
[201502] => W 02
[201503] => W 03
[201504] => W 04
[201505] => W 05
)
*/
They don't work anymore as my dates may be in different years.
Having a database with all dates for 10 years in advance is not an option .
To lengthen a year when the week is> 52 is not conceivable , since I don't know if the year contains 51, 52 or 53 weeks.
Edit: here is the whole background of the story
I need to plot a weekly graph based on data. Initially I had the year and quarter of the year as just the "date".
I came up with a function to give me the year (year + quarter):
function getYearweeks($year, $quarter) {
$min = ($quarter - 1) * 3 +1;
$max = $quarter * 3 + 1;
$dmin = $year . "-" . (($min < 10)?"0" . $min:$min) . "-01";
$dmax = (($max > 12)?($year + 1) . "-01-01":$year . "-" . (($max < 10)?"0" . $max:$max) . "-01");
$q = "SELECT YEARWEEK('" . $dmin . "',1) AS yw_min,
YEARWEEK(DATE_SUB('" . $dmax . "', INTERVAL 1 DAY),1) AS yw_max";
$r = mysql_query($q) or die (mysql_error()."<br />".$q."<br />");
$d = mysql_fetch_assoc($r);
$yw_min = $d['yw_min'];
$yw_max = $d['yw_max'];
$yearweeks = array();
for ($i = $yw_min; $i <= $yw_max; $i++) {
$yearweeks[$i] = "W " . substr($i, 4, 6);
}
return $yearweeks;
}
The problem is that the client now wants to crawl; I added an "interval" parameter to my function, which is the number of weeks to add (or below) to the original date (year + quarter):
function getYearweeks($year, $quarter, $interval=0) {
$min = ($quarter - 1) * 3 +1;
$max = $quarter * 3 + 1;
$dmin = $year . "-" . (($min < 10)?"0" . $min:$min) . "-01";
$dmax = (($max > 12)?($year + 1) . "-01-01":$year . "-" . (($max < 10)?"0" . $max:$max) . "-01");
$q = "SELECT YEARWEEK(DATE_ADD('" . $dmin . "', INTERVAL " . $interval . " WEEK),1) AS yw_min,
YEARWEEK(DATE_SUB(DATE_ADD('" . $dmax . "', INTERVAL " . $interval . " WEEK), INTERVAL 1 DAY),1) AS yw_max";
$r = mysql_query($q) or die (mysql_error()."<br />".$q."<br />");
$d = mysql_fetch_assoc($r);
$yw_min = $d['yw_min'];
$yw_max = $d['yw_max'];
// mess up when we are between two different years
$yearweeks = array();
for ($i = $yw_min; $i <= $yw_max; $i++) {
$yearweeks[$i] = "W " . substr($i, 4, 6);
}
return $yearweeks;
}
source to share
You can do this in PHP using the class DatePeriod
. Supply two dates (start and end, respectively) at DateTime
when creating the object as well DateInterval
as at DatePeriod
.
Example:
$dates = array();
$begin = new DateTime('2014-09-01'); // beginning then from
$end = new DateTime('2015-02-02'); // end
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
foreach($daterange as $date) {
$dates[$date->format('o W')] = 'Week ' . $date->format('W'); // iso date
}
echo '<pre>';
print_r($dates);
Should output:
Array
(
[2014 36] => Week 36
[2014 37] => Week 37
[2014 38] => Week 38
[2014 39] => Week 39
[2014 40] => Week 40
[2014 41] => Week 41
[2014 42] => Week 42
[2014 43] => Week 43
[2014 44] => Week 44
[2014 45] => Week 45
[2014 46] => Week 46
[2014 47] => Week 47
[2014 48] => Week 48
[2014 49] => Week 49
[2014 50] => Week 50
[2014 51] => Week 51
[2014 52] => Week 52
[2014 01] => Week 01
[2015 01] => Week 01
[2015 02] => Week 02
[2015 03] => Week 03
[2015 04] => Week 04
[2015 05] => Week 05
[2015 06] => Week 06
)
For more information visit the DatePeriod
class in the tutorial.
source to share