소스 검색

DateHelper

Alexander Musikhin 10 달 전
부모
커밋
20ad3830e4
1개의 변경된 파일68개의 추가작업 그리고 0개의 파일을 삭제
  1. 68 0
      app/Helpers/DateHelper.php

+ 68 - 0
app/Helpers/DateHelper.php

@@ -0,0 +1,68 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Helpers;
+
+use Carbon\Exceptions\InvalidDateException;
+use Illuminate\Support\Carbon;
+use Illuminate\Support\Str;
+
+class DateHelper
+{
+    const DB_FORMAT = 'Y-MM-DD';
+    const HUMAN_TEXT_FORMAT = 'D MMMM Y';
+    const HUMAN_DATE_FORMAT = 'DD.MM.Y';
+    const MINIMAL_RANDOM_YEAR = 1970;
+    const SECONDS_IN_YEAR = 365 * 24 * 60 * 60;
+
+    const YEARS = 'год|года|лет';
+    const MONTHS = 'месяц|месяца|месяцев';
+    const DAYS = 'день|дня|дней';
+
+    /**
+     * @param string $date
+     * @param bool $monthAsNumber
+     * @return string
+     */
+    public static function getHumanDate(string $date, bool $monthAsNumber = false): string
+    {
+        $format = ($monthAsNumber) ? self::HUMAN_DATE_FORMAT : self::HUMAN_TEXT_FORMAT;
+        $dt = Carbon::parse($date);
+        return Str::lower($dt->isoFormat($format));
+    }
+
+    /**
+     * Случайная дата за последние X лет в формате YYYY-MM-DD
+     * @param int|null $last_years
+     * @return string
+     */
+    public static function getRandomDate(?int $last_years = 7): string
+    {
+        if (($last_years > ((int)date('Y') - self::MINIMAL_RANDOM_YEAR)) || ($last_years < 0)) {
+            throw new InvalidDateException('last_years', $last_years, 1);
+        }
+        $seconds = rand(0, $last_years * self::SECONDS_IN_YEAR); // случайное число секунд в диапазоне от 0 до Х лет
+        return Carbon::createFromTimestamp(strtotime('now') - $seconds)->isoFormat(self::DB_FORMAT);
+    }
+
+    /**
+     * @param string $date
+     * @return string
+     */
+    public static function getDateForDB(string $date): string
+    {
+        return Carbon::parse($date)->isoFormat(self::DB_FORMAT);
+    }
+
+    /**
+     * @param string $date
+     * @param int $months
+     * @return string
+     */
+    public static function addMonths(string $date, int $months): string
+    {
+        return Carbon::parse($date)->addMonths($months)->isoFormat(self::DB_FORMAT);
+    }
+
+
+}