【问题标题】:PHP date conversion using strtotime使用 strtotime 的 PHP 日期转换
【发布时间】:2015-05-15 15:21:01
【问题描述】:

我有一个 ymd 格式的日期。 2014-05-28 的示例我实际上有:140528。

我想将其转换为日期(c")格式。示例:2014-05-28T00:00:00-04:00

由于我的 php 版本,我无法使用 DateTime::createFromFormat。所以我尝试了strtotime,但没有成功:

$d[14]="140528";
$deliveryDate = date('ymd', $d[14]);
$d[14] = date("c", strtotime($deliveryDate));

strtotime 无法识别原始日期格式,因为它返回 1969 年的日期。

【问题讨论】:

    标签: php date strtotime date-formatting


    【解决方案1】:

    将其分解为多个部分,然后从这些部分中形成一个有效日期。然后strtotime() 就可以了。

    $badDate = "140528";
    $date = sprintf("20%s-%s-%s",
        substr($badDate, 0, 2),
        substr($badDate, 2, 2),
        substr($badDate, 4, 2)
    );
    
    echo date("c", strtotime($date));
    

    Demo

    【讨论】:

    • 效果很好,谢谢!不喜欢硬编码的“20”位,但我想它会在接下来的 85 年中使用。
    猜你喜欢
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    相关资源
    最近更新 更多