PHP解析日期字符串 [英] PHP Parse Date String

查看:125
本文介绍了PHP解析日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我该怎么做?

有没有特别的日期功能我应该使用?还有别的什么?

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

谢谢!

推荐答案

explode 将会为此做些窍门:

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

这篇关于PHP解析日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆