我有以下格式的日期(yyyymmdd,18751104,19140722)…什么是最简单的方法将它转换为日期()….或使用mktime()和子串我最好的选择…?
解决方法:
// both lines output 813470400
echo strtotime("19951012"), "\n",
strtotime("12 October 1995");
?>
您可以将结果作为第二个参数传递给date()以自行重新格式化日期:
// prints 1995 Oct 12
echo date("Y M d", strtotime("19951012"));
?>
注意
strtotime()将在1970年初的Unix时代之前的日期失败.
作为替代方案,将适用于1970年之前的日期:
// 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
?>
标签:php,datetime,date,format
来源: https://codeday.me/bug/20190926/1819304.html