将 m/d/Y 格式的日期字符串解析为 $m、$d 和 $y 变量

发布于 2024-08-02 02:57:50 字数 184 浏览 8 评论 0原文

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

$date = "08/20/2009";

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

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

我该怎么做?

我应该使用专用的日期函数吗?

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 dedicated date function I should be using?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

许你一世情深 2024-08-09 02:57:50

一种方法是这样做:

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

One way would be to do this:

$time = strtotime($date);
$m = date('m', $time);
$d = date('d', $time);
$y = date('Y', $time);
飘过的浮云 2024-08-09 02:57:50

explode 可以解决这个问题:

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

或者,您可以在一行(请参阅评论 - 感谢幸运):

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

explode will do the trick for that:

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

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

list($m, $d, $y) = explode("/", $date);
不必了 2024-08-09 02:57:50

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

例如

<?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"));
?>

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.

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"));
?>
淡写薰衣草的香 2024-08-09 02:57:50

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

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

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

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

If you have a given format you should use a date object.

$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().

$newFormat = $date->format('d-m-Y');
偏闹i 2024-08-09 02:57:50

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

尝试 strtotime。

就像是:

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

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

Try strtotime.

Something like:

if(($iTime = strtotime($strDate))!==false)
{
 echo date('m', $iTime);
 echo date('d', $iTime);
 echo date('y', $iTime);
}
ゃ人海孤独症 2024-08-09 02:57:50

这个怎么样:

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

快速的一个班轮。

how about this:

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

A quick one liner.

木緿 2024-08-09 02:57:50

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

例如:

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

For internationalized date parsing, see 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'));
酒与心事 2024-08-09 02:57:50

多米尼克的答案很好,但是如果日期始终采用相同的格式,您可以使用这个:

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

并且不必使用数组或爆炸

Bill H

Dominic's answer is good, but IF the date is ALWAYS in 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文