Midday(); // true * Carbon::parse('2019-02-28 12:00:01')->isMidday(); // false * ``` * * @return bool */ public function isMidday() { /* @var CarbonInterface $this */ return $this->rawFormat('G:i:s') === static::$midDayAt.':00:00'; } /** * Checks if the (date)time string is in a given format. * * @example * ``` * Carbon::hasFormat('11:12:45', 'h:i:s'); // true * Carbon::hasFormat('13:12:45', 'h:i:s'); // false * ``` * * @SuppressWarnings(PHPMD.EmptyCatchBlock) * * @param string $date * @param string $format * * @return bool */ public static function hasFormat($date, $format) { try { // Try to create a DateTime object. Throws an InvalidArgumentException if the provided time string // doesn't match the format in any way. static::rawCreateFromFormat($format, $date); // createFromFormat() is known to handle edge cases silently. // E.g. "1975-5-1" (Y-n-j) will still be parsed correctly when "Y-m-d" is supplied as the format. // To ensure we're really testing against our desired format, perform an additional regex validation. // Preg quote, but remove escaped backslashes since we'll deal with escaped characters in the format string. $quotedFormat = str_replace('\\\\', '\\', preg_quote($format, '/')); // Build the regex string $regex = ''; for ($i = 0; $i < strlen($quotedFormat); ++$i) { // Backslash – the next character does not represent a date token so add it on as-is and continue. // We're doing an extra ++$i here to increment the loop by 2. if ($quotedFormat[$i] === '\\') { $char = $quotedFormat[++$i]; $regex .= $char === '\\' ? '\\\\' : $char; continue; } $regex .= strtr($quotedFormat[$i], static::$regexFormats); } return (bool) preg_match('/^'.str_replace('/', '\\/', $regex).'$/', $date); } catch (InvalidArgumentException $e) { } return false; } /** * Returns true if the current date matches the given string. * * @example * ``` * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('2019')); // true * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('2018')); // false * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('2019-06')); // true * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('06-02')); // true * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('2019-06-02')); // true * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('Sunday')); // true * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('June')); // true * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('12:23')); // true * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('12:23:45')); // true * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('12:23:00')); // false * var_dump(Carbon::parse('2019-06-02 12:23:45')->is('12h')); // true * var_dump(Carbon::parse('2019-06-02 15:23:45')->is('3pm')); // true * var_dump(Carbon::parse('2019-06-02 15:23:45')->is('3am')); // false * ``` * * @param string $tester day name, month name, hour, date, etc. as string * * @return bool */ public function is(string $tester) { $tester = trim($tester); if (preg_match('/^\d+$/', $tester)) { return $this->year === intval($tester); } if (preg_match('/^\d{3,}-\d{1,2}$/', $tester)) { return $this->isSameMonth(static::parse($tester)); } if (preg_match('/^\d{1,2}-\d{1,2}$/', $tester)) { return $this->isSameDay(static::parse($this->year.'-'.$tester)); } $modifier = preg_replace('/(\d)h$/i', '$1:00', $tester); /* @var CarbonInterface $max */ $median = static::parse('5555-06-15 12:30:30.555555')->modify($modifier); $current = $this->copy(); /* @var CarbonInterface $other */ $other = $this->copy()->modify($modifier); if ($current->eq($other)) { return true; } if (preg_match('/\d:\d{1,2}:\d{1,2}$/', $tester)) { return $current->startOfSecond()->eq($other); } if (preg_match('/\d:\d{1,2}$/', $tester)) { return $current->startOfMinute()->eq($other); } if (preg_match('/\d(h|am|pm)$/', $tester)) { return $current->startOfHour()->eq($other); } if (preg_match( '/^(january|february|march|april|may|june|july|august|september|october|november|december)\s+\d+$/i', $tester )) { return $current->startOfMonth()->eq($other->startOfMonth()); } $units = [ 'month' => [1, 'year'], 'day' => [1, 'month'], 'hour' => [0, 'day'], 'minute' => [0, 'hour'], 'second' => [0, 'minute'], 'microsecond' => [0, 'second'], ]; foreach ($units as $unit => [$minimum, $startUnit]) { if ($median->$unit === $minimum) { $current = $current->startOf($startUnit); break; } } return $current->eq($other); } }