Converting string to Date and DateTime

后端 未结 10 1642
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 17:07

If I have a PHP string in the format of mm-dd-YYYY (for example, 10-16-2003), how do I properly convert that to a Date and then a DateTime

相关标签:
10条回答
  • 2020-11-22 17:37

    If you wish to accept dates using American ordering (month, date, year) for European style formats (using dash or period as day, month, year) while still accepting other formats, you can extend the DateTime class:

    /**
     * Quietly convert European format to American format
     *
     * Accepts m-d-Y, m-d-y, m.d.Y, m.d.y, Y-m-d, Y.m.d
     * as well as all other built-in formats
     * 
     */
    class CustomDateTime extends DateTime 
    {
      public function __construct(string $time="now", DateTimeZone $timezone = null) 
      {
        // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y (substr avoids microtime error)
        $time = str_replace(['-','.'], '/', substr($time, 0, 10)) . substr($time, 10 );
    
        parent::__construct($time, $timezone);
      }
    }
    
    // usage:
    $date = new CustomDateTime('7-24-2019');
    print $date->format('Y-m-d');
    
    // => '2019-07-24'
    

    Or, you can make a function to accept m-d-Y and output Y-m-d:

    /**
     * Accept dates in various m, d, y formats and return as Y-m-d
     * 
     * Changes PHP's default behaviour for dates with dashes or dots.
     * Accepts:
     *   m-d-y, m-d-Y, Y-m-d,
     *   m.d.y, m.d.Y, Y.m.d,
     *   m/d/y, m/d/Y, Y/m/d,
     *   ... and all other formats natively supported 
     * 
     * Unsupported formats or invalid dates will generate an Exception
     * 
     * @see https://www.php.net/manual/en/datetime.formats.date.php PHP formats supported
     * @param  string $d various representations of date
     * @return string    Y-m-d or '----' for null or blank
     */
    function asYmd($d) {
      if(is_null($d) || $d=='') { return '----'; }
    
      // convert m-d-y or m.d.y to m/d/y to avoid PHP parsing as d-m-Y
      $d = str_replace(['-','.'], '/', $d);
    
      return (new DateTime($d))->format('Y-m-d');
    }
    
    // usage:
    
    <?= asYmd('7-24-2019') ?>
    
    // or
    
    <?php echo asYmd('7-24-2019'); ?>
    
    0 讨论(0)
  • 2020-11-22 17:42

    If you have format dd-mm-yyyy then in PHP it won't work as expected. In PHP document they have below guideline.

    Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

    So, you just can't use as you wish. When your try to use dd/mm/yyyy format with this then it will remove FALSE. You can tweak with the following.

    $date = "23/02/2013";
    $timestamp = strtotime($date);
    if ($timestamp === FALSE) {
      $timestamp = strtotime(str_replace('/', '-', $date));
    }
    echo $timestamp; // prints 1361577600
    
    0 讨论(0)
  • 2020-11-22 17:42

    Since no one mentioned this, here's another way:

    $date = date_create_from_format("m-d-Y", "10-16-2003")->format("Y-m-d");
    
    0 讨论(0)
  • 2020-11-22 17:49

    to create date from any string use:
    $date = DateTime::createFromFormat('d-m-y H:i', '01-01-01 01:00'); echo $date->format('Y-m-d H:i');

    0 讨论(0)
提交回复
热议问题