略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Supported Date and Time Formats

2024-11-15

Supported Date and Time Formats

目录

This section describes all the different formats that the DateTimeImmutable, DateTime, date_create_immutable(), date_create(), date_parse(), and strtotime() parser understands. The formats are grouped by section. In most cases formats from different sections, separated by whitespace, comma or dot, can be used in the same date/time string. For each of the supported formats, one or more examples are given, as well as a description for the format. Characters in single quotes in the formats are case-insensitive ('t' could be t or T), characters in double quotes are case-sensitive ("T" is only T).

A general set of rules should be taken into account.

  1. The parser, allows for each unit (year, month, day, hour, minute, second) the full range of values. For a year that's just 4 digits, for a month that's 0-12, day is 0-31 and for hour and minute it's 0-59.
  2. 60 is allowed for seconds, as sometimes date strings with that leapsecond do show up. But PHP implements Unix time where "60" is not a valid second number and hence it overflows.
  3. strtotime() returns false if any number is outside of the ranges, and DateTimeImmutable::__construct() throws an exception.
  4. If a string contains a date, all time elements are reset to 0.
  5. All less-significant time elements are reset to 0 if any part of a time is present in the given string.
  6. The parser is dumb, and doesn't do any checks to make it faster (and more generic).
  7. There is an additional check if an invalid date is provided:

    <?php
    $res 
    date_parse("2015-09-31");
    var_dump($res["warnings"]);
    ?>

    以上例程会输出:

    array(1) {
      [11] =>
      string(27) "The parsed date was invalid"
    }
    
  8. It is already possible to handle the edge cases, but then DateTimeImmutable::createFromFormat() must be used while supplying the correct format.

    <?php
    $res 
    DateImmutable::createFromFormat("Y-m-d""2015-09-34");
    var_dump($res);

    以上例程会输出:

    class DateTime#1 (3) {
      public $date =>
      string(26) "2015-10-04 17:24:43.000000"
      public $timezone_type =>
      int(3)
      public $timezone =>
      string(13) "Europe/London"
    }
    
add a noteadd a note

User Contributed Notes 1 note

up
21
Ray.Paseur sometimes uses Gmail
5 years ago
When you've got external inputs that do not strictly follow the formatting and disambiguation rules, you may still be able to use the static method ::createFromFormat() to create a usable DateTime object

<?php
/**
* Date values separated by slash are assumed to be in American order: m/d/y
* Date values separated by dash are assumed to be in European order: d-m-y
* Exact formats for date/time strings can be injected with ::createFromFormat()
*/
error_reporting(E_ALL);

// THIS IS INVALID, WOULD IMPLY MONTH == 19
$external = "19/10/2016 14:48:21";

// HOWEVER WE CAN INJECT THE FORMATTING WHEN WE DECODE THE DATE
$format = "d/m/Y H:i:s";
$dateobj = DateTime::createFromFormat($format, $external);

$iso_datetime = $dateobj->format(Datetime::ATOM);
echo
"SUCCESS: $external EQUALS ISO-8601 $iso_datetime";

// MAN PAGE: http://php.net/manual/en/datetime.createfromformat.php

官方地址:https://www.php.net/manual/en/datetime.formats.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3