Skip to content

Instantly share code, notes, and snippets.

@itorgov
Last active March 6, 2020 10:29
Show Gist options
  • Select an option

  • Save itorgov/fdeb793d5612fc859a0b3b1c72f85d6c to your computer and use it in GitHub Desktop.

Select an option

Save itorgov/fdeb793d5612fc859a0b3b1c72f85d6c to your computer and use it in GitHub Desktop.

Revisions

  1. itorgov revised this gist Mar 6, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions DateService.php
    Original 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 Начальная дата.
    * @param DateTime $endDate Конечная дата.
    * @param string $intervalSize Размер интервалов (используйте константы).
    * @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
    */
  2. itorgov revised this gist Aug 25, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion DateService.php
    Original 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 getSubDates(DateTime $startDate, DateTime $endDate, $intervalSize)
    public static function getSubIntervals(DateTime $startDate, DateTime $endDate, $intervalSize)
    {
    $dates = [];

  3. itorgov revised this gist Aug 25, 2016. 1 changed file with 14 additions and 16 deletions.
    30 changes: 14 additions & 16 deletions DateService.php
    Original file line number Diff line number Diff line change
    @@ -2,40 +2,38 @@

    class DateService
    {
    const ONE_WEEK_INTERVAL = '6';
    const ONE_WEEK_INTERVAL = '+6 days';

    /**
    * Метод из начальной и конечной дат делает массив с интервалами заданного размера.
    * TODO: Текущий алгоритм мне не очень нравится, но пока что только так придумал.
    *
    * @param DateTime $startDate Начальная дата.
    * @param DateTime $endDate Конечная дата.
    * @param string $intervalSize Размер интервалов (используйте константы).
    *
    * @return array
    */
    public static function getSubDates(DateTime $startDate, DateTime $endDate, $intervalSize)
    {
    $dates = [];

    while (true)
    {
    $dates = [];

    while (true) {
    $firstDate = clone($startDate);
    $startDate->modify('+' . $intervalSize . ' days');
    $secondDate = clone($startDate);
    $startDate->modify($intervalSize);
    $secondDate = clone($startDate);

    $dates[] = [
    'start' => $firstDate,
    'end' => $secondDate >= $endDate ? $endDate : $secondDate
    'end' => $secondDate >= $endDate ? $endDate : $secondDate
    ];

    $startDate->modify('+1 days');

    if ($firstDate >= $endDate || $secondDate >= $endDate)
    {

    if ($firstDate >= $endDate || $secondDate >= $endDate) {
    break;
    }
    }

    return $dates;
    }
    }
  4. itorgov revised this gist Aug 25, 2016. 1 changed file with 20 additions and 20 deletions.
    40 changes: 20 additions & 20 deletions DateService.php
    Original 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)
    $dates = [];

    while (true)
    {
    break;
    $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;
    }

    return $dates;
    }
    }
  5. itorgov revised this gist Aug 25, 2016. 1 changed file with 20 additions and 38 deletions.
    58 changes: 20 additions & 38 deletions DateService.php
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    class DateService
    {
    const SEVEN_DAYS_INTERVAL = 'P7D';
    const ONE_WEEK_INTERVAL = '6';

    /**
    * Метод из начальной и конечной дат делает массив с интервалами заданного размера.
    @@ -13,47 +13,29 @@ class DateService
    * @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;
    }
    public static function getSubDates(DateTime $startDate, DateTime $endDate, $intervalSize)
    {
    $dates = [];

    if (end($dates) < $endDate) {
    $dates[] = $endDate;
    }
    while (true)
    {
    $firstDate = clone($startDate);
    $startDate->modify('+' . $intervalSize . ' days');
    $secondDate = clone($startDate);

    $subIntervals = [];
    $index = 0;
    $dates[] = [
    'start' => $firstDate,
    'end' => $secondDate >= $endDate ? $endDate : $secondDate
    ];

    foreach ($dates as $date) {
    if (!isset($subIntervals[$index])) {
    $subIntervals[$index] = [];
    }
    $startDate->modify('+1 days');

    if (!array_key_exists('start', $subIntervals[$index])) {
    $subIntervals[$index]['start'] = $date;
    } else {
    $subIntervals[$index]['end'] = $date;
    $index++;
    }
    if ($firstDate >= $endDate || $secondDate >= $endDate)
    {
    break;
    }

    return $subIntervals;
    }

    return $dates;
    }
    }
  6. itorgov created this gist Aug 25, 2016.
    59 changes: 59 additions & 0 deletions DateService.php
    Original 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;
    }
    }