strtotime()& date()将日期转换为与之前相同的格式时的奇怪行为 [英] strtotime() & date() weird behaviour when converting date in to same format as it was before

查看:153
本文介绍了strtotime()& date()将日期转换为与之前相同的格式时的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将日期格式转换为 mm-dd-yyyy 我不知道当前的日期格式是什么是动态的,所以如果我有动态日期格式已经在 mm-dd-yyyy 然后 date()函数返回下面outout


I have to convert date format in to mm-dd-yyyy I don't know what is the current date format it is dynamic so if I have dynamic date format is already in mm-dd-yyyy then date() function is returning below outout

    $date='02-13-2011';
    echo date('m-d-Y',strtotime($date));

输出是

  01-01-1970

?>

http://codepad.org/AFZ6jel7

所以我必须检查日期是否已经在 mm-dd-yyyy 然后不要应用日期格式。
有没有其他方法呢?可能会在这些函数中传递另一个参数或类似的东西。

So I have to check if the date is already in mm-dd-yyyy then do not apply date formatting. Is there any other way do this? may be passing one other parameter in these functions or something similar.

谢谢。

推荐答案

我强烈怀疑这是导致问题的原因:

I strongly suspect that this is what's causing the problem:


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

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文档。)

你有破折号分隔符,所以它假设为dmy格式,解析为13个月,因此有效地失败。

You've got dash separators, so it's assuming d-m-y format, parsing it as a month of 13, and thus effectively failing.

选项我可以想到离开我的头顶,而不是一个PHP开发人员:

Options I can think of off the top of my head, without being a PHP developer:


  • 如果有一个功能允许你明确说出什么格式的字符串使用

  • 将格式更改为使用斜杠

  • 更改格式以使用dmy而不是mdy

这篇关于strtotime()& date()将日期转换为与之前相同的格式时的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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