PHP 解析日期字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1279061/
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 01:51:12  来源:igfitidea点击:

PHP Parse Date String

phpdateparsing

提问by Dominic Rodger

If I've got a date string:

如果我有一个日期字符串:

$date = "08/20/2009";

And I want to separate each part of the date:

我想分开日期的每个部分:

$m = "08";
$d = "20";
$y = "2009";

How would I do so?

我该怎么做?

Is there a special date function I should be using? Or something else?

我应该使用特殊的日期函数吗?或者是其他东西?

Thanks!

谢谢!

回答by Doug Hays

One way would be to do this:

一种方法是这样做:

$time = strtotime($date);
$m = date('m', $time);
$d = date('d', $time);
$y = date('Y', $time);

回答by Dominic Rodger

explodewill do the trick for that:

explode会做的伎俩:

$pieces = explode("/", $date);
$d = $pieces[1];
$m = $pieces[0];
$y = $pieces[2];

Alternatively, you could do it in one line (see comments - thanks Lucky):

或者,您可以在一行中完成(请参阅评论 - 感谢 Lucky):

list($m, $d, $y) = explode("/", $date);

回答by Glen Tankersley

Check out PHP's date_parse function. Unless you're sure input will always be in a consistent format, AND you validate it first, it is much more stable and flexible, (not to mention easier), to let PHP try to parse the format for you.

查看 PHP 的date_parse 函数。除非您确定输入将始终采用一致的格式,并且您首先对其进行验证,否则它会更加稳定和灵活(更不用说更容易了),让 PHP 尝试为您解析格式。

e.g.

例如

<?php
//Both inputs should return the same Month-Day-Year
print_r(date_parse("2012-1-12 51:00:00.5"));
print_r(date_parse("1/12/2012"));
?>

回答by gagarine

If you have a given format you should use a dateobject.

如果您有给定的格式,则应使用日期对象。

$date = DateTime::createFromFormat('m/d/Y', '08/20/2009');
$m = $date->format('m');
$d = $date->format('d');
$y = $date->format('Y');

Note you can certainly use only one call to DateTime::format().

请注意,您当然只能使用一次对DateTime::format() 的调用。

$newFormat = $date->format('d-m-Y');

回答by Daniel

Is it always like that? Or will it be in any sort of file format?

总是这样吗?或者它会是任何类型的文件格式?

Try strtotime.

尝试strtotime。

Something like:

就像是:

if(($iTime = strtotime($strDate))!==false)
{
 echo date('m', $iTime);
 echo date('d', $iTime);
 echo date('y', $iTime);
}

回答by Carson Myers

how about this:

这个怎么样:

list($m, $d, $y) = explode("/", $date);

A quick one liner.

一个快速的班轮。

回答by George Lund

For internationalized date parsing, see IntlDateFormatter::parse - http://php.net/manual/en/intldateformatter.parse.php

有关国际化日期解析,请参阅 IntlDateFormatter::parse - http://php.net/manual/en/intldateformatter.parse.php

For example:

例如:

$f = new \IntlDateFormatter('en_gb', \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE);
$dt = new \DateTime();
$dt->setTimestamp($f->parse('1/2/2015'));

回答by Bill H

Dominic's answer is good, but IFthe date is ALWAYSin the same format you could use this:

多米尼克的回答很好,但如果日期总是采用相同的格式,您可以使用:

$m = substr($date,0,2);
$d = substr($date,3,2);
$y = substr($date,-4);

and not have to use an array or explode

并且不必使用数组或爆炸

Bill H

比尔·H