Created
January 16, 2012 20:43
-
-
Save brianlmoon/1622938 to your computer and use it in GitHub Desktop.
Get weeks from a range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Given two timestamps returns the Y-m-d ranges for each week within the range. | |
* | |
* @param int $start_ts Starting timestamp | |
* @param int $end_ts Ending timestamp | |
* @param bool $full_weeks If true, full weeks will be returned meaning | |
* the start and end will be adjusted to create full weeks | |
* @return array | |
* | |
* @test | |
* $mydates = DateFormat::get_week_ranges(strtotime("2011-11-01"), strtotime("2011-11-14")); | |
* echo json_encode($mydates); | |
* echo "\n"; | |
* $mydates = DateFormat::get_week_ranges(strtotime("2011-11-01"), strtotime("2011-11-14"), false); | |
* echo json_encode($mydates); | |
* @expects | |
* [["2011-10-30","2011-11-05"],["2011-11-06","2011-11-12"],["2011-11-13","2011-11-19"]] | |
* [["2011-11-01","2011-11-05"],["2011-11-06","2011-11-12"],["2011-11-13","2011-11-14"]] | |
* @end | |
* | |
*/ | |
public static function get_week_ranges($start_ts, $end_ts, $full_weeks = true) { | |
// normalize times to noon | |
$start_ts = strtotime(date("Y-m-d", $start_ts)." 12:00:00"); | |
$end_ts = strtotime(date("Y-m-d", $end_ts)." 12:00:00"); | |
$start_week_day = date("w", $start_ts); | |
if($start_week_day != 0){ | |
$adj_start_ts = $start_ts - (86400 * $start_week_day); | |
} else { | |
$adj_start_ts = $start_ts; | |
} | |
$end_week_day = date("w", $end_ts); | |
if($end_week_day != 6){ | |
$adj_end_ts = $end_ts + (86400 * (6 - $end_week_day)); | |
} else { | |
$adj_end_ts = $end_ts; | |
} | |
$ranges = self::get_date_ranges($adj_start_ts, $adj_end_ts, 7); | |
if(!$full_weeks){ | |
$start_date = date("Y-m-d", $start_ts); | |
if($ranges[0][0] < $start_date){ | |
$ranges[0][0] = $start_date; | |
} | |
$max = count($ranges) - 1; | |
$end_date = date("Y-m-d", $end_ts); | |
if($ranges[$max][1] > $end_date){ | |
$ranges[$max][1] = $end_date; | |
} | |
} | |
return $ranges; | |
} | |
/** | |
* Given two timestamps and a integer, returns the Y-m-d range, separated by days | |
* of that integer | |
* | |
* @param int $start_ts Starting timestamp | |
* @param int $end_ts Ending timestamp | |
* @return array | |
* | |
* @test | |
* $mydates = DateFormat::get_date_ranges(strtotime("2010-11-01"),strtotime("2010-11-14"),7); | |
* echo json_encode($mydates); | |
* @expects | |
* [["2010-11-01","2010-11-07"],["2010-11-08","2010-11-14"]] | |
* @end | |
* | |
*/ | |
public static function get_date_ranges($start_ts, $end_ts, $days_per_range) { | |
$date_ranges = array(); | |
// normalize times to noon | |
$start_ts = strtotime(date("Y-m-d", $start_ts)." 12:00:00"); | |
$end_ts = strtotime(date("Y-m-d", $end_ts)." 12:00:00"); | |
$days = $days_per_range - 1; | |
for($real_start_ts = $start_ts; $real_start_ts <= $end_ts; $real_start_ts += (86400 * $days_per_range)) { | |
$real_end_ts = strtotime(date("Y-m-d", $real_start_ts) . " 12:00:00 + $days days"); | |
if($real_end_ts - $end_ts >= 0) { | |
$real_end_ts = $end_ts; | |
} | |
$date_ranges[] = array(date("Y-m-d", $real_start_ts), date("Y-m-d", $real_end_ts)); | |
} | |
return $date_ranges; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment