* @link https://TheTempusProject.com/Core * @license https://opensource.org/licenses/MIT [MIT LICENSE] */ namespace TheTempusProject\Bedrock\Functions; use DateTime; use DateTimeZone; class Date { private static $date = null; private static $errorLog = []; private static $errorLogFull = []; private static $errorLogUser = []; public static function formatTimestamp( $type, $timestamp ) { if ( stripos( $type, 'date' ) ) { $dateFormat = self::getDateFormat(); } elseif ( stripos( $type, 'time' ) ) { $dateFormat = self::getTimeFormat(); } else { $dateFormat = self::getDateFormat() . ' ' . self::getTimeFormat(); } $time = intval( $timestamp ); $dt = new DateTime( self::getTimezone() ); $dt->setTimestamp( $time ); return $dt->format( $dateFormat ); } public static function applyTimezoneToTimestamp( $timestamp ) { $timestamp = intval( $timestamp ); $date = new DateTime(); $date->setTimestamp( $timestamp ); $timezone = new DateTimeZone( self::getTimezone() ); $date->setTimezone( $timezone ); $formattedDate = $date->format('Y-m-d H:i:s'); return $formattedDate; } public static function applyUtcToDate( $date ) { $timestamp = intval( strtotime( $date ) ); $startDate = new DateTime( $date, new DateTimeZone( self::getTimezone() ) ); $startDate->setTimezone( new DateTimeZone( 'UTC' ) ); $firstSecond = $startDate->getTimestamp(); return $firstSecond; } public static function getReadableDate( $timestamp, $with_timezone = false ) { if ( $with_timezone ) { $date = new DateTime(); $date->setTimestamp( $timestamp ); $timezone = new DateTimeZone( self::getTimezone() ); $date->setTimezone( $timezone ); $formattedDate = $date->format('Y-m-d H:i:s'); return $formattedDate; } return date( 'Y-m-d H:i:s', $timestamp ); } public static function convertToTimestampWithTimezone( $dateString, $timeString, $timezoneString ) { $datetimeString = $dateString . ' ' . $timeString; $date = new DateTime($datetimeString, new DateTimeZone($timezoneString)); $timestamp = $date->getTimestamp(); return $timestamp; } public static function getDateBreakdown( $timestamp, $with_timezone = false ) { $timestamp = intval( $timestamp ); $init = date('Y-m-d H:i:s', $timestamp); if ( true === $with_timezone ) { $readableDate = self::applyTimezoneToTimestamp( $timestamp ); } else { $readableDate = date('Y-m-d H:i:s', $timestamp); } $date = date( 'Y-m-d', strtotime( $readableDate ) ); $time = date( 'H:i', strtotime( $readableDate ) ); return [ 'time' => $time, 'date' => $date, ]; } public static function determineDateInput( $with_timezone = false) { $currentTimestamp = time(); $currentDateString = date( 'F d, Y', $currentTimestamp ); // Prioritize inputs: Post > Get > defaults if ( Input::exists('time') ) { $time = Input::post('time') ? Input::post('time') : Input::get('time'); } else { $time = '00:00'; } if ( Input::exists('date') ) { $date = Input::post('date') ? Input::post('date') : Input::get('date'); } else { $date = $currentDateString; } if ( Input::exists('hour') ) { $hour = Input::post('hour') ? Input::post('hour') : Input::get('hour'); } else { $hour = '00'; } if ( Input::exists('minute') ) { $minute = Input::post('minute') ? Input::post('minute') : Input::get('minute'); } else { $minute = '00'; } if ( Input::exists('day') ) { $day = Input::post('day') ? Input::post('day') : Input::get('day'); } else { $day = date( 'd', $currentTimestamp ); } if ( Input::exists('month') ) { $month = Input::post('month') ? Input::post('month') : Input::get('month'); } else { $month = date( 'M', $currentTimestamp ); } if ( Input::exists('year') ) { $year = Input::post('year') ? Input::post('year') : Input::get('year'); } else { $year = date( 'Y', $currentTimestamp ); } // prioritize a time input over individual hours and minutes if ( '00:00' == $time ) { $time = $hour . ':' . $minute; } // prioritize a date input over individual day, month, year if ( $currentDateString == $date ) { $inputTimestamp = strtotime( $time. ' ' . $month . ' ' . $day . ', ' . $year ); /** * Its possible to select IE: 31, Feb; in navigation. * This will back the day down until it finds an acceptable date. */ $intDay = intval( $day ); $intMonth = intval( date( 'm', $inputTimestamp ) ); $intYear = intval( $year ); // 29 if ( ! checkdate( $intMonth, $intDay, $intYear ) ) { $day = $intDay - 1; } // 30 if ( ! checkdate( $intMonth, $intDay, $intYear ) ) { $day = $intDay - 1; } // 31 if ( ! checkdate( $intMonth, $intDay, $intYear ) ) { $day = $intDay - 1; } $inputTimestamp = strtotime( $time. ' ' . $month . ' ' . $day . ', ' . $year ); } else { $inputTimestamp = strtotime( $time . ' ' . $date ); } $timestamp = self::getReadableDate( $inputTimestamp, $with_timezone ); return $timestamp; } /** * Application getters */ public static function getDateFormat() { $format = DEFAULT_DATE_FORMAT; if ( !empty( self::$activePrefs ) && !empty( self::$activePrefs['dateFormat'] ) ) { $format = self::$activePrefs['dateFormat']; } return $format; } public static function getTimeFormat() { $format = DEFAULT_TIME_FORMAT; if ( !empty( self::$activePrefs ) && !empty( self::$activePrefs['timeFormat'] ) ) { $format = self::$activePrefs['timeFormat']; } return $format; } public static function getTimezone() { $format = DEFAULT_TIMEZONE; if ( !empty( self::$activePrefs ) && !empty( self::$activePrefs['timezone'] ) ) { $format = self::$activePrefs['timezone']; } return $format; } /** * Current getters */ public static function getCurrentDay() { $date = new DateTime(); $date->setTimestamp( time() ); $timezone = new DateTimeZone( self::getTimezone() ); $date->setTimezone( $timezone ); $hourNow = $date->format('d'); return $hourNow; } public static function getCurrentMonth() { $date = new DateTime(); $date->setTimestamp( time() ); $timezone = new DateTimeZone( self::getTimezone() ); $date->setTimezone( $timezone ); $hourNow = $date->format('M'); return $hourNow; } public static function getCurrentYear() { $date = new DateTime(); $date->setTimestamp( time() ); $timezone = new DateTimeZone( self::getTimezone() ); $date->setTimezone( $timezone ); $hourNow = $date->format('Y'); return $hourNow; } public static function getCurrentHour() { $date = new DateTime(); $date->setTimestamp( time() ); $timezone = new DateTimeZone( self::getTimezone() ); $date->setTimezone( $timezone ); $hourNow = $date->format('H'); return $hourNow; } /** * Relative Dates */ public static function getDayStartTimestamp( $timestamp = 0, $with_timezone = false ) { if ( empty( $timestamp ) ) { $timestamp = self::determineDateInput(); } $day = date( 'd', $timestamp ); $month = date( 'M', $timestamp ); $year = date( 'Y', $timestamp ); $startTime = '00:00:00'; $firstSecond = date( 'U', strtotime( "$startTime $day $month $year" ) ); if ( $with_timezone ) { $specificDateString = $year.'-'.$month.'-'.$day; $startDate = new DateTime( $specificDateString . ' ' . $startTime, new DateTimeZone( self::getTimezone() ) ); $startDate->setTimezone( new DateTimeZone( 'UTC' ) ); $firstSecond = $startDate->getTimestamp(); } return $firstSecond; } public static function getDayEndTimestamp( $timestamp = 0, $with_timezone = false ) { if ( empty( $timestamp ) ) { $timestamp = self::determineDateInput(); } $day = date( 'd', $timestamp ); $month = date( 'M', $timestamp ); $year = date( 'Y', $timestamp ); $endTime = '23:59:59'; $lastSecond = date( 'U', strtotime( "$endTime $day $month $year" ) ); if ( $with_timezone ) { $specificDateString = $year.'-'.$month.'-'.$day; $endDate = new DateTime( $specificDateString . ' ' . $endTime, new DateTimeZone( self::getTimezone() ) ); $endDate->setTimezone( new DateTimeZone( 'UTC' ) ); $lastSecond = $endDate->getTimestamp(); } return $lastSecond; } public static function getWeekStartTimestamp( $timestamp = 0, $with_timezone = false ) { if ( empty( $timestamp ) ) { $timestamp = self::determineDateInput(); } // $timestamp = intval( $timestamp ); $startTime = '00:00:00'; // find the first sunday in the week containing the date if ( date( 'N', $timestamp ) == 7 ) { $firstDate = $timestamp; } else { $firstDate = strtotime('last Sunday', $timestamp); } $firstSecond = date( 'Y-M-d '. $startTime, $firstDate ); if ( $with_timezone ) { $startDate = new DateTime( $firstSecond, new DateTimeZone( self::getTimezone() ) ); $startDate->setTimezone( new DateTimeZone( 'UTC' ) ); $firstSecond = $startDate->getTimestamp(); } else { $firstSecond = strtotime( $firstSecond ); } return $firstSecond; } public static function getWeekEndTimestamp( $timestamp = 0, $with_timezone = false ) { if ( empty( $timestamp ) ) { $timestamp = self::determineDateInput(); } // $timestamp = intval( $timestamp ); $endTime = '23:59:59'; // find the last saturday in the week containing the date if ( date( 'N', $timestamp ) == 6 ) { $lastDate = $timestamp; } else { $lastDate = strtotime( 'next Saturday', $timestamp ); } $lastSecond = date( 'Y-M-d '. $endTime, $lastDate ); if ( $with_timezone ) { $endDate = new DateTime( $lastSecond, new DateTimeZone( self::getTimezone() ) ); $endDate->setTimezone( new DateTimeZone( 'UTC' ) ); $lastSecond = $endDate->getTimestamp(); } else { $lastSecond = strtotime( $lastSecond ); } return $lastSecond; } public static function getMonthStartTimestamp( $timestamp = 0, $with_timezone = false ) { if ( empty( $timestamp ) ) { $timestamp = self::determineDateInput(); } $startTime = '00:00:00'; $year = date( 'Y', $timestamp ); $month = date( 'M', $timestamp ); $firstDayUnix = strtotime( "$startTime $month 01 $year" ); // find the first sunday in the week containing the date if ( date( 'N', $firstDayUnix ) == 7 ) { $firstDate = $firstDayUnix; } else { $firstDate = strtotime('last Sunday', $firstDayUnix); } $firstSecond = date( 'Y-M-d '. $startTime, $firstDate ); if ( $with_timezone ) { $startDate = new DateTime( $firstSecond, new DateTimeZone( self::getTimezone() ) ); $startDate->setTimezone( new DateTimeZone( 'UTC' ) ); $firstSecond = $startDate->getTimestamp(); } else { $firstSecond = strtotime( $firstSecond ); } return $firstSecond; } public static function getMonthEndTimestamp( $timestamp = 0, $with_timezone = false ) { if ( empty( $timestamp ) ) { $timestamp = self::determineDateInput(); } $endTime = '23:59:59'; $year = date( 'Y', $timestamp ); // Find last day of month $month = date( 'm', $timestamp ); $lastDay = cal_days_in_month( CAL_GREGORIAN, $month, $year ); $month = date( 'M', $timestamp ); $lastDayUnix = strtotime( "$endTime $month $lastDay $year" ); // find the last saturday in the week containing the date if ( date( 'N', $lastDayUnix ) == 6 ) { $lastDate = $lastDayUnix; } else { $lastDate = strtotime('next Saturday', $lastDayUnix); } $lastSecond = date( 'Y-M-d '. $endTime, $lastDate ); if ( $with_timezone ) { $endDate = new DateTime( $lastSecond, new DateTimeZone( self::getTimezone() ) ); $endDate->setTimezone( new DateTimeZone( 'UTC' ) ); $lastSecond = $endDate->getTimestamp(); } else { $lastSecond = strtotime( $lastSecond ); } return $lastSecond; } public static function getYearStartTimestamp( $timestamp = 0, $with_timezone = false ) { if ( empty( $timestamp ) ) { $timestamp = self::determineDateInput(); } $startTime = '00:00:00'; $year = date( 'Y', $timestamp ); $firstDayUnix = strtotime( "$startTime January 01 $year" ); $firstSecond = date( 'Y-M-d '. $startTime, $firstDayUnix ); if ( $with_timezone ) { $startDate = new DateTime( $firstSecond, new DateTimeZone( self::getTimezone() ) ); $startDate->setTimezone( new DateTimeZone( 'UTC' ) ); $firstSecond = $startDate->getTimestamp(); } else { $firstSecond = strtotime( $firstSecond ); } return $firstSecond; } public static function getYearEndTimestamp( $timestamp = 0, $with_timezone = false ) { if ( empty( $timestamp ) ) { $timestamp = self::determineDateInput(); } $endTime = '23:59:59'; $year = date( 'Y', $timestamp ); $lastDayUnix = strtotime( "$endTime December 31 $year" ); $lastSecond = date( 'Y-M-d '. $endTime, $lastDayUnix ); if ( $with_timezone ) { $endDate = new DateTime( $lastSecond, new DateTimeZone( self::getTimezone() ) ); $endDate->setTimezone( new DateTimeZone( 'UTC' ) ); $lastSecond = $endDate->getTimestamp(); } else { $lastSecond = strtotime( $lastSecond ); } return $lastSecond; } }