[英]strtotime() & date() weird behaviour when converting date in to same format as it was before
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我必须将日期格式转换为mm-dd-yyyy
我不知道当前的日期格式是什么,它是动态的,所以如果我的动态日期格式已经在mm-dd-yyyy
那么date()
函数将在下面返回出局
$date='02-13-2011';
echo date('m-d-Y',strtotime($date));
output is输出是
01-01-1970
?>
http://codepad.org/AFZ6jel7 http://codepad.org/AFZ6jel7
So I have to check if the date is already in mm-dd-yyyy
then do not apply date formatting.所以我必须检查日期是否已经在mm-dd-yyyy
然后不应用日期格式。 Is there any other way do this?有没有其他方法可以做到这一点? may be passing one other parameter in these functions or something similar.可能在这些函数中传递另一个参数或类似的东西。
Thanks.谢谢。
I strongly suspect that this is what's causing the problem:我强烈怀疑这是导致问题的原因:
Dates in the m/d/y or dmy 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; m/d/y 或 dmy 格式的日期通过查看各个组件之间的分隔符来消除歧义:如果分隔符是斜杠 (/),则假定为美国 m/d/y; whereas if the separator is a dash (-) or a dot (.), then the European dmy format is assumed.而如果分隔符是破折号 (-) 或点 (.),则假定为欧洲 dmy 格式。
(Found in the strtotime documentation .) (在strtotime 文档中找到。)
You've got dash separators, so it's assuming dmy format, parsing it as a month of 13, and thus effectively failing.您有破折号分隔符,因此它假定为 dmy 格式,将其解析为 13 个月,因此实际上失败了。
Options I can think of off the top of my head, without being a PHP developer:如果不是 PHP 开发人员,我能想到的选项:
If there's a function which allows you to explicitly say what format the string is in, use that.如果有一个函数可以让您明确说明字符串的格式,请使用它。
For example DateTime::createFromFormat()
例如DateTime::createFromFormat()
用 / for mdY 替换 - 会有所帮助。
echo date('m-d-Y',strtotime(str_replace('-', '/', $date)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.