Strtotime() 不适用于 dd/mm/YYYY 格式 [英] Strtotime() doesn't work with dd/mm/YYYY format

查看:21
本文介绍了Strtotime() 不适用于 dd/mm/YYYY 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很喜欢 strtotime() 函数,但是用户手册没有给出支持的日期格式的完整描述.strtotime('dd/mm/YYYY') 不起作用,它只适用于 mm/dd/YYYY 格式.

如果我有 dd/mm/YYYY 格式的日期,我如何将它转换为 YYYY-mm-dd?我可以通过使用 explode() 函数来实现,但我认为有更好的解决方案.

解决方案

这里是简化的解决方案:

$date = '25/05/2010';$date = str_replace('/', '-', $date);echo date('Y-m-d', strtotime($date));

结果:

2010-05-25

strtotime 文档 内容如下:<块引用>

m/d/ydmy 格式的日期通过查看它们之间的分隔符来消除歧义各个组成部分:如果分隔符是斜杠(/),则假定为美国m/d/y;而如果分隔符是破折号 (-) 或点 (.),则欧洲的 dmy 格式假设.

I really like the strtotime() function, but the user manual doesn't give a complete description of the supported date formats. strtotime('dd/mm/YYYY') doesn't work, it works only with mm/dd/YYYY format.

If I have date in dd/mm/YYYY format, how can I convert it to YYYY-mm-dd? I can do it by using explode() function, but I think there are better solutions.

解决方案

Here is the simplified solution:

$date = '25/05/2010';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));

Result:

2010-05-25

The strtotime documentation reads:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

这篇关于Strtotime() 不适用于 dd/mm/YYYY 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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