setTimestamp($timestamp); return $dt; } /** * Get UTC today @00:00:00 . * Helper method to generate date for mysql * * @return DateTime * @throws Exception */ public static function todayStart(){ $d2 = new DateTime(); return DateUtil::utcDate($d2); } /** * @param $date \DateTime optional. The date to set as utc date. If not set, new DateTime() will be used * @return DateTime the datetime object with time reset and utc timezone */ public static function utcDate($date = null){ $d2 = isset($date ) ? $date : new DateTime(); $d2 = DateUtil::withTimeZoneUTC($d2); return DateUtil::resetTime($d2); } /** * @param $date \DateTime optional. If not set,defaults to : new DateTime() . * @return DateTime */ public static function utcDateTime($date = null){ $d2 = isset($date ) ? $date : new DateTime(); return DateUtil::withTimeZoneUTC($d2); } /** * @param $date \DateTime * @return DateTime */ public static function withTimeZoneUTC( $date = null){ $d2 = isset($date ) ? $date : new DateTime(); $d2->setTimezone( new DateTimeZone('UTC') ); return $d2; } /** * @param $dateTime \DateTime * @return \DateTime */ public static function resetTime($dateTime){ $dateTime->setTime(0, 0); return $dateTime; } /** * Get UTC t @00:00:00 . * Helper method to generate date for mysql * * @return DateTime * @throws Exception */ public static function tomorrowStart( $date = null){ $d2 = isset($date) ? $date : new DateTime(); $d2->add(new DateInterval('P1D')); return DateUtil::utcDate($d2); } public static function addMonth($timestamp, $monthCount = 1) { if ($timestamp instanceof DateTime) { $d1 = $timestamp; } else { $d1 = new DateTime(); if (isset($timestamp)) { $d1->setTimestamp($timestamp); } } $monthToAdd = $monthCount; $year = $d1->format('Y'); $month = $d1->format('n'); $day = $d1->format('d'); $year += floor($monthToAdd / 12); $monthToAdd %= 12; $month += $monthToAdd; if ($month > 12) { $year++; $month %= 12; if ($month === 0) { $month = 12; } } if (!checkdate($month, $day, $year)) { $d2 = DateTime::createFromFormat('Y-n-j', $year . '-' . $month . '-1'); $d2->modify('last day of'); } else { $d2 = DateTime::createFromFormat('Y-n-d', $year . '-' . $month . '-' . $day); } $d2->setTime(0, 0); return $d2; } /** * @param $dateTimeObject * @return string * @throws InvalidConfigException */ public static function formatDateTimeUtc($dateTimeObject) { $formatter = new Formatter; $formatter->datetimeFormat = 'php:Y-m-d H:i:s'; $formatter->timeZone = 'UTC'; return $formatter->asDatetime($dateTimeObject); } /** * @param $dateTimeObject * @return string * @throws InvalidConfigException */ public static function formatDateUtc($dateTimeObject) { $formatter = new Formatter; $formatter->datetimeFormat = 'php:Y-m-d'; $formatter->timeZone = 'UTC'; return $formatter->asDatetime($dateTimeObject); } public static function parseDate($dateString){ $date = DateTime::createFromFormat('Y.m.d', $dateString, new DateTimeZone( 'UTC')); $date->setTime(0, 0); return $date; } public static function parseDateTime($dateTimeString){ return DateTime::createFromFormat('Y-m-d H:i:s', $dateTimeString, new DateTimeZone( 'UTC')); } /** * @param integer $weekDay Numeric representation of the day of the week. @See https://www.php.net/manual/en/function.date.php * @return string */ public static function getLocalDayName($weekDay){ $translations = [ 0 => Yii::t('common', 'Sunday'), 1 => Yii::t('common', 'Monday'), 2 => Yii::t('common', 'Tuesday'), 3 => Yii::t('common', 'Wednesday'), 4 => Yii::t('common', 'Thursday'), 5 => Yii::t('common', 'Friday'), 6 => Yii::t('common', 'Saturday'), 7 => Yii::t('common', 'Sunday'), ]; return $translations[$weekDay]; } /** * @param $dateTime \DateTime * @return void */ public static function resetSeconds($dateTime){ return $dateTime->setTime($dateTime->format("H"), $dateTime->format("i"),0); } /** * @param $dateTime \DateTime * @param $minutes integer * @return mixed * @throws Exception */ public static function addMinutes($dateTime, $minutes){ return $dateTime->add(new \DateInterval('PT' . $minutes . 'M')); } public static function minusMinutes($dateTime, $minutes){ return $dateTime->sub(new \DateInterval('PT' . $minutes . 'M')); } }