mea*_*gar 37
使用strtotime()含日期到字符串转换Unix时间戳:
<?php
// both lines output 813470400
echo strtotime("19951012"), "\n",
strtotime("12 October 1995");
?>
Run Code Online (Sandbox Code Playgroud)
您可以将结果作为第二个参数传递date()给自己重新格式化日期:
<?php
// prints 1995 Oct 12
echo date("Y M d", strtotime("19951012"));
?>
Run Code Online (Sandbox Code Playgroud)
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
?>
Run Code Online (Sandbox Code Playgroud)
就个人而言,我只是使用substr(),因为无论如何它可能是最轻的方法.
但这是一个采用日期的函数,您可以在其中指定格式.它返回一个关联数组,所以你可以这样做(未经测试):
$parsed_date = date_parse_from_format('Ymd', $date);
$timestamp = mktime($parsed_date['year'], $parsed_date['month'], $parsed_date['day']);
Run Code Online (Sandbox Code Playgroud)
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));
Run Code Online (Sandbox Code Playgroud)