Skip to content

Instantly share code, notes, and snippets.

@andrewdavidcostello
Created October 16, 2012 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewdavidcostello/3898583 to your computer and use it in GitHub Desktop.
Save andrewdavidcostello/3898583 to your computer and use it in GitHub Desktop.
PHP Calculate Monthly Intervals
<?php
$start_at = '2012-11-15';
$end_at = strtotime('2012-11-15');
$final_date = strtotime('2013-01-22');
$dates = array();
# Calculate intervals.
while($end_at < $final_date){
if(strtotime('2012-11-15') != $end_at)
$start_at = date("Y-m-d", strtotime("+1 day", $end_at));
$end_of_month = date("Y-m-t", strtotime($start_at));
if(strtotime($end_of_month) > strtotime('2013-01-22'))
$end_of_month = '2013-01-22';
$dates[] = array(
"start"=>$start_at,
"end"=>$end_of_month,
);
$end_at = strtotime($end_of_month);
}
?>
<pre>
<?=print_r($dates)?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment