用PHP将日期从yyyymmdd格式转换为Y-m-d格式

3

http://php.net/manual/zh/function.date-create-from-format.php 和 http://php.net/manual/zh/datetime.format.php - Marc B
1
执行搜索所花费的时间比输入这个问题要少。 - user557846
这是最基本的字符串操作。 - miken32
1个回答

10
使用strtotime()将包含日期的字符串转换为Unix时间戳:
<?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"));
?>

1
这太简单了,我现在感到有点不好意思问。谢谢。 - JuanBonnett

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接