Last active
March 6, 2020 10:29
-
-
Save itorgov/fdeb793d5612fc859a0b3b1c72f85d6c to your computer and use it in GitHub Desktop.
Revisions
-
itorgov revised this gist
Mar 6, 2020 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -7,11 +7,11 @@ class DateService const ONE_YEAR_INTERVAL = '+364 days'; /** * This method get two dates (start and end of an interval) and length of every subinterval you need. * * @param DateTime $startDate Start of an original interval. * @param DateTime $endDate End of an original interval. * @param string $intervalSize Length of every subinterval (use constants, please). * * @return array */ -
itorgov revised this gist
Aug 25, 2016 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -3,6 +3,8 @@ class DateService { const ONE_WEEK_INTERVAL = '+6 days'; const THIRTY_DAYS_INTERVAL = '+29 days'; const ONE_YEAR_INTERVAL = '+364 days'; /** * Метод из начальной и конечной дат делает массив с интервалами заданного размера. @@ -13,7 +15,7 @@ class DateService * * @return array */ public static function getSubIntervals(DateTime $startDate, DateTime $endDate, $intervalSize) { $dates = []; -
itorgov revised this gist
Aug 25, 2016 . 1 changed file with 14 additions and 16 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,40 +2,38 @@ class DateService { const ONE_WEEK_INTERVAL = '+6 days'; /** * Метод из начальной и конечной дат делает массив с интервалами заданного размера. * * @param DateTime $startDate Начальная дата. * @param DateTime $endDate Конечная дата. * @param string $intervalSize Размер интервалов (используйте константы). * * @return array */ public static function getSubDates(DateTime $startDate, DateTime $endDate, $intervalSize) { $dates = []; while (true) { $firstDate = clone($startDate); $startDate->modify($intervalSize); $secondDate = clone($startDate); $dates[] = [ 'start' => $firstDate, 'end' => $secondDate >= $endDate ? $endDate : $secondDate ]; $startDate->modify('+1 days'); if ($firstDate >= $endDate || $secondDate >= $endDate) { break; } } return $dates; } } -
itorgov revised this gist
Aug 25, 2016 . 1 changed file with 20 additions and 20 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -14,28 +14,28 @@ class DateService * @return array */ public static function getSubDates(DateTime $startDate, DateTime $endDate, $intervalSize) { $dates = []; while (true) { $firstDate = clone($startDate); $startDate->modify('+' . $intervalSize . ' days'); $secondDate = clone($startDate); $dates[] = [ 'start' => $firstDate, 'end' => $secondDate >= $endDate ? $endDate : $secondDate ]; $startDate->modify('+1 days'); if ($firstDate >= $endDate || $secondDate >= $endDate) { break; } } return $dates; } } -
itorgov revised this gist
Aug 25, 2016 . 1 changed file with 20 additions and 38 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ class DateService { const ONE_WEEK_INTERVAL = '6'; /** * Метод из начальной и конечной дат делает массив с интервалами заданного размера. @@ -13,47 +13,29 @@ class DateService * @param string $intervalSize Размер интервалов (используйте константы). * @return array */ public static function getSubDates(DateTime $startDate, DateTime $endDate, $intervalSize) { $dates = []; while (true) { $firstDate = clone($startDate); $startDate->modify('+' . $intervalSize . ' days'); $secondDate = clone($startDate); $dates[] = [ 'start' => $firstDate, 'end' => $secondDate >= $endDate ? $endDate : $secondDate ]; $startDate->modify('+1 days'); if ($firstDate >= $endDate || $secondDate >= $endDate) { break; } } return $dates; } } -
itorgov created this gist
Aug 25, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ <?php class DateService { const SEVEN_DAYS_INTERVAL = 'P7D'; /** * Метод из начальной и конечной дат делает массив с интервалами заданного размера. * TODO: Текущий алгоритм мне не очень нравится, но пока что только так придумал. * * @param DateTime $startDate Начальная дата. * @param DateTime $endDate Конечная дата. * @param string $intervalSize Размер интервалов (используйте константы). * @return array */ public static function getSubIntervals(DateTime $startDate, DateTime $endDate, $intervalSize) { $dateInterval = new DateInterval($intervalSize); $dateRange = new DatePeriod($startDate, $dateInterval, $endDate); $dates = []; $isFirst = true; /** @var DateTime $date */ foreach ($dateRange as $date) { if ($isFirst) { $dates[] = $date; $isFirst = false; continue; } $previousDate = clone($date); $previousDate->modify('-1 day'); $dates[] = $previousDate; $dates[] = $date; } if (end($dates) < $endDate) { $dates[] = $endDate; } $subIntervals = []; $index = 0; foreach ($dates as $date) { if (!isset($subIntervals[$index])) { $subIntervals[$index] = []; } if (!array_key_exists('start', $subIntervals[$index])) { $subIntervals[$index]['start'] = $date; } else { $subIntervals[$index]['end'] = $date; $index++; } } return $subIntervals; } }