Created
April 25, 2018 09:31
-
-
Save egulhan/13803cf59b3cc9391accf8f66487441f to your computer and use it in GitHub Desktop.
Find weeks between two dates
This file contains hidden or 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 | |
/** | |
* 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