简单问题:如何将日期(08-17-2011)分割成月,日,年? PHP [英] Simple Question: How to split date (08-17-2011) into month, day, year? PHP
本文介绍了简单问题:如何将日期(08-17-2011)分割成月,日,年? PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为$ orderdate的变量,它设置为一个日期格式,如mm-dd-yyyy。
在PHP中,我将该变量分为感谢您的帮助。
解决方案
如果您确定输入值的格式,则:
$ orderdate = explode(' - ',$ orderdate);
$ month = $ orderdate [0];
$ day = $ orderdate [1];
$ year = $ orderdate [2];
您还可以使用 preg_match()
:
if(preg_match('#^(\d {2}) - (\d {2}) - (\d {4})$#',$ orderdate,$ matches)){
$ month = $ matches [1];
$ day = $ matches [2];
$ year = $ matches [3];
} else {
echo'invalid format';
}
此外,您可以使用 checkdate()
验证日期。
I have a variable called $orderdate and it is set to a date format like this mm-dd-yyyy.
In PHP how would I split this variable into $month, $day, $year?
Thanks for your help.
解决方案
If you're sure about the format of input value, then:
$orderdate = explode('-', $orderdate);
$month = $orderdate[0];
$day = $orderdate[1];
$year = $orderdate[2];
You could also use preg_match()
:
if (preg_match('#^(\d{2})-(\d{2})-(\d{4})$#', $orderdate, $matches)) {
$month = $matches[1];
$day = $matches[2];
$year = $matches[3];
} else {
echo 'invalid format';
}
Additionally, you can use checkdate()
to validate the date.
这篇关于简单问题:如何将日期(08-17-2011)分割成月,日,年? PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文