Skip to content

Instantly share code, notes, and snippets.

@egulhan
Created April 25, 2018 09:31
Show Gist options
  • Save egulhan/13803cf59b3cc9391accf8f66487441f to your computer and use it in GitHub Desktop.
Save egulhan/13803cf59b3cc9391accf8f66487441f to your computer and use it in GitHub Desktop.
Find weeks between two dates
<?php
/**
* Finds weeks by two dates
* @param $startDate
* @param $endDate
* @return array
*/
public static function findWeeksBetweenTwoDates($startDate, $endDate)
{
$weeks = [];
while (strtotime($startDate) <= strtotime($endDate)) {
$oldStartDate = $startDate;
$startDate = date('Y-m-d', strtotime('+7 day', strtotime($startDate)));
if (strtotime($startDate) > strtotime($endDate)) {
$week = [$oldStartDate, $endDate];
}
else {
$week = [$oldStartDate, date('Y-m-d', strtotime('-1 day', strtotime($startDate))) ];
}
$weeks[] = $week;
}
return $weeks;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment