【问题标题】:Converting to date in PHP from yyyymmdd format从 yyyymmdd 格式转换为 PHP 中的日期
【发布时间】:2011-01-21 15:26:06
【问题描述】:

我有以下格式的日期(yyyymmdd、18751104、19140722)...将其转换为 date() 的最简单方法是什么...或者使用 mktime() 和子字符串是我的最佳选择...?

【问题讨论】:

    标签: php datetime date format


    【解决方案1】:

    看看strptime。它可以解析strftime生成的时间/日期。

    文档中的示例:

    <?php
    $format = '%d/%m/%Y %H:%M:%S'; $strf = strftime($format);
    
    echo "$strf\n";
    
    print_r(strptime($strf, $format));
    ?>
    

    上面的例子会输出类似于:

    03/10/2004 15:54:19
    
    Array (
        [tm_sec] => 19
        [tm_min] => 54
        [tm_hour] => 15
        [tm_mday] => 3
        [tm_mon] => 9
        [tm_year] => 104
        [tm_wday] => 0
        [tm_yday] => 276
        [unparsed] =>
    )
    

    【讨论】:

    • @mickmackusa:谢谢,我已经更新了过去的答案(呸,快 10 岁了!)
    • 您是合作的 1% 中的一员,在意识到之后实际上会改进内容。感谢您改进 Stack Overflow。如果您可以定制 sn-p 来专门处理 OP 提供的格式,那就更好了。 (Ymd)
    【解决方案2】:

    (PHP 5 >= 5.3.0,PHP 7):

    您可以通过以下方式获取 DateTime 实例:

    $dateTime = \DateTime::createFromFormat('Ymd|', '18951012');
    

    并将其转换为时间戳:

    $timestamp = $dateTime->getTimestamp();
    // -> -2342217600
    

    【讨论】:

      【解决方案3】:

      使用strtotime()将包含日期的字符串转换为Unix timestamp

      <?php
      // both lines output 813470400
      echo strtotime("19951012"), "\n",
           strtotime("12 October 1995");
      ?>
      

      您可以将结果作为第二个参数传递给date() 以自行重新格式化日期:

      <?php
      // prints 1995 Oct 12
      echo date("Y M d", strtotime("19951012"));
      ?>
      

      注意

      strtotime() 将失败,日期早于 1970 年初的 Unix 纪元。

      作为替代方案,它适用于 1970 年之前的日期:

      <?php
      // Returns the year as an offset since 1900, negative for years before
      $parts = strptime("18951012", "%Y%m%d");
      $year = $parts['tm_year'] + 1900; // 1895
      $day = $parts['tm_mday']; // 12
      $month = $parts['tm_mon']; // 10
      ?>
      

      【讨论】:

      • @Serhiy strtotime() 对 1970 年之前的日期返回 false,因此本质上您调用的是 date(0),它将返回 1969 年 12 月 31 日 23:59:59
      • $month 实际上返回“9”,而不是“10”,因为 srtptime 给出的月份为“自一月以来的月份 (0-11)”。所以你需要 $parts['tm_mon']+1 才能达到“10”。
      【解决方案4】:

      感谢所有答案,但 1900 年的问题似乎困扰着我得到的每一个回复。如果将来有人发现它对他们有用,这是我正在使用的功能的副本。

      public static function nice_date($d){
          $ms = array(
                 'January',
                 'February',
                 'March',
                 'April',
                 'May',
                 'June',
                 'July',
                 'August',
                 'September',
                 'October',
                 'November',
                 'December'
          );
      
          $the_return = '';
          $the_month = abs(substr($d,4,2));
          if ($the_month != 0) {
              $the_return .= $ms[$the_month-1];
          }
      
          $the_day = abs(substr($d,6,2));
          if ($the_day != 0){
              $the_return .= ' '.$the_day;
          }
      
          $the_year = substr($d,0,4);
          if ($the_year != 0){
              if ($the_return != '') {
                  $the_return .= ', ';
              }
              $the_return .= $the_year;
          }
      
          return $the_return;
      }
      

      【讨论】:

        【解决方案5】:

        就个人而言,我只会使用 substr() 因为它可能是最简单的方法。

        但这里有一个接受日期的函数,您可以指定日期的格式。它返回一个关联数组,因此您可以这样做(未经测试):

        $parsed_date = date_parse_from_format('Ymd', $date);
        $timestamp = mktime($parsed_date['year'], $parsed_date['month'], $parsed_date['day']);
        

        http://uk.php.net/manual/en/function.date-parse-from-format.php

        虽然我必须说,我认为没有比简单更容易或更有效的方法:

        mktime(substr($date, 0, 4), substr($date, 4, 2), substr($date, 6, 2));
        

        【讨论】:

        • 我投票支持这个作为答案...但是 mktime 与其他答案一样遇到 1970 年的问题...而且 mktime 的安排有点偏离...
        猜你喜欢
        • 2018-10-27
        • 1970-01-01
        • 2014-07-03
        • 1970-01-01
        • 1970-01-01
        • 2020-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多