从 yyyymmdd 格式转换为 PHP 中的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2344933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:06:46  来源:igfitidea点击:

Converting to date in PHP from yyyymmdd format

phpdatetimedateformat

提问by Serhiy

I have dates in the following format (yyyymmdd, 18751104, 19140722)... what's the easiest way to convert it to date().... or is using mktime() and substrings my best option...?

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

回答by meagar

Use strtotime()to convert a string containing a date into a Unix timestamp:

使用strtotime()含日期到字符串转换Unix时间戳

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

You can pass the result as the second parameter to date()to reformat the date yourself:

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

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

Note

笔记

strtotime()will fail with dates before the Unix epoch at the start of 1970.

strtotime()将在 1970 年初 Unix 纪元之前的日期失败。

As an alternative which will work with dates before 1970:

作为一种替代方法,它适用于 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
?>

回答by Teekin

Personally, I'd just use substr() because it's probably the lightest way to do it anyway.

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

But here's a function that takes a date, of which you can specify the format. It returns an associative array, so you could do for example (untested):

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

$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

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

Although I must say, I don't find that any easier or more effective than simply:

尽管我必须说,我觉得没有比简单的更容易或更有效的了:

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

回答by knittl

Have a look at strptime. It can parse a time/date generated with strftime.

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

Example from the docs:

文档中的示例:

<?php
$format = '%d/%m/%Y %H:%M:%S'; $strf = strftime($format);

echo "$strf\n";

print_r(strptime($strf, $format));
?>

The above example will output something similar to:

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] =>
)
<?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] =>
)

回答by Serhiy

Well thanks for all the answers but the 1900 problem seems to plague every response I got. Here is a copy of the function I am using should someone find it useful for them in the future.

嗯,感谢所有的答案,但 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;
}

回答by Artisan72

(PHP 5 >= 5.3.0, PHP 7):

(PHP 5 >= 5.3.0,PHP 7):

You can get a DateTime instance with:

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

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

and convert it to a timestamp:

并将其转换为时间戳:

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