* * @return string The formatted datetime string. * * @throws ErrorException If the provided datetime string is invalid. */ public static function format($date, $args = []) { $args = wp_parse_args($args, [ 'include_time' => false, 'exclude_year' => false, 'short_month' => false, 'separator' => ' ', 'date_format' => self::getDateFormat(), 'time_format' => self::getTimeFormat() ]); // If the date is numeric, treat it as a Unix timestamp if (is_numeric($date)) { $dateTime = new \DateTime('@' . $date, new \DateTimeZone('UTC')); $dateTime->setTimezone(self::getTimezone()); } else { $dateTime = new \DateTime($date, self::getTimezone()); } $format = $args['date_format']; if ($args['include_time'] === true) { $format = $args['date_format'] . $args['separator'] . $args['time_format']; } if ($args['exclude_year']) { $format = preg_replace('/(,\s?Y|Y\s?,|Y[, \/-]?|[, \/-]?Y)/i', '', $format); } if ($args['short_month']) { $format = str_replace('F', 'M', $format); } return $dateTime->format($format); } /** * Check is Valid date * * @param $date * @return bool */ public static function isValidDate($date) { if (empty($date)) { return false; } if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $date) && strtotime($date) !== false) { return true; } return false; } /** * Checks if the given date is today or in the future. * * @param string $date * * @return bool */ public static function isTodayOrFutureDate($date) { $today = date('Y-m-d'); if (!$date || strtotime($date) === false) { return false; } $inputDate = date('Y-m-d', strtotime($date)); return ($inputDate >= $today); } }