我必须将日期格式转换为 mm-dd-yyyy
我不知道当前日期格式是什么,它是动态的所以如果我有动态日期格式已经在 mm-dd-yyyy
然后 date()
函数返回低于 outout
$date='02-13-2011';
echo date('m-d-Y',strtotime($date));
输出是
01-01-1970
?>
所以我必须检查日期是否已经在 mm-dd-yyyy
中,然后不应用日期格式。
还有其他方法吗?可能在这些函数中传递另一个参数或类似的东西。
谢谢。
最佳答案
我强烈怀疑这是导致问题的原因:
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 documentation 中找到。)
你有破折号分隔符,所以它假设 d-m-y 格式,将其解析为 13 的月份,因此实际上失败了。
即使不是 PHP 开发人员,我也能想到一些选项:
如果有一个函数允许您明确说明字符串的格式,请使用它。
- 更改格式以使用斜杠
- 更改格式以使用 d-m-y 而不是 m-d-y
关于php - strtotime() & date() 将日期转换为与以前相同的格式时出现奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5101605/