strtotime()将非现有日期转换为另一个日期 [英] strtotime() converts a non existing date to another date
问题描述
我正在从用户输入的日期,月份和年份值创建时间戳。
I am building a timestamp from the date, month and year values entered by users.
假设用户输入一些错误的值,日期是 02-2012不存在,那么我得到一个错误的回报。但在这里,它将其转换为附近的另一个日期。确切地说:02-03-2012..
Suppose that the user inputs some wrong values and the date is "31-02-2012" which does not exist, then I have to get a false return. But here its converting it to another date nearby. Precisely to: "02-03-2012"..
我不想这样做..
$str = "31-02-2012";
echo date("d-m-Y",strtotime($str)); // Outputs 02-03-2012
有人可以帮忙吗?如果日期不是原始的,我不想要返回时间戳。
Can anyone help? I dont want a timestamp to be returned if the date is not original.
推荐答案
这是因为 strtotime )
在 -
中有麻烦,因为它们用于表示短语,如 -1周
等等...
That's because strtotime()
has troubles with -
since they are used to denote phrase like -1 week
, etc...
尝试
$str = '31-02-2012';
echo date('d-m-Y', strtotime(str_replace('-', '/', $str)));
然而 31-02-2012
不是有效的英文格式,应该是 02-31-2012
。
However 31-02-2012
is not a valid English format, it should be 02-31-2012
.
如果您有 PHP> = 5.3 ,您可以使用 createFromFormat
:
If you have PHP >= 5.3, you can use createFromFormat
:
$str = '31-02-2012';
$d = DateTime::createFromFormat('d-m-Y', $str);
echo $d->format('d-m-Y');
这篇关于strtotime()将非现有日期转换为另一个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!