| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- declare(strict_types=1);
- namespace App\Helpers;
- use Carbon\Exceptions\InvalidDateException;
- use DateTime;
- use Exception;
- 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));
- }
- public static function getHumanDayOfWeek(string $date): string
- {
- $dt = Carbon::parse($date);
- return Str::ucfirst($dt->dayName);
- }
- /**
- * Случайная дата за последние 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);
- }
- /**
- * @param string $string
- * @return bool
- */
- public static function isDate(string $string): bool
- {
- if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$string)) {
- return true;
- } else {
- return false;
- }
- }
- public static function getDateOfWeek(int $year, int $weekNumber, $dayOfWeek = 1): string
- {
- $date = new DateTime();
- // Set the date to the specified year and week number, with Monday as the day (1)
- $date->setISODate($year, $weekNumber, $dayOfWeek);
- return $date->format('Y-m-d');
- }
- public static function excelDateToISODate(int $excelDate): string
- {
- $ts = ($excelDate - 25569) * 86400;
- $date = new DateTime();
- $date->setTimestamp($ts);
- return $date->format('Y-m-d');
- }
- /**
- * @throws \DateMalformedStringException
- */
- public static function ISODateToExcelDate(string $isoDate): int
- {
- $date = new DateTime($isoDate);
- $timestamp = $date->getTimestamp();
- $excelDate = ($timestamp / 86400) + 25569;
- return (int) round($excelDate);
- }
- }
|