如何确定值是否是 PHP 中的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4226096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine if value is a date in PHP
提问by Todd Moses
I am working with arrays of values in PHP. Some of these values may include a date in various string formats.
我正在处理 PHP 中的值数组。其中一些值可能包括各种字符串格式的日期。
I need to convert dates in multiple formats to their numerical equivalent (Unix timestamp). The problem is being able to determine if the string is a date.
我需要将多种格式的日期转换为其等效的数字(Unix 时间戳)。问题是能够确定字符串是否是日期。
Using
使用
if (($timestamp = strtotime($str)) === false)
will check for a valid date from a string but how do I determine if the value should even be validated as a date?
将检查字符串中的有效日期,但如何确定该值是否应该被验证为日期?
Example:
例子:
$x = {1,2,3,"4","11/12/2009","22/12/2000",true,false};
foreach($x as $value)
{
if(is_bool($value))
if(is_string($value))
if(is_numeric($value))
if(is_date($value)) ?
...
}
In short, is there an easy way to check if a string value is a date?
简而言之,是否有一种简单的方法可以检查字符串值是否为日期?
回答by Pekka
In short, is there an easy way to check if a string value is a date?
简而言之,是否有一种简单的方法可以检查字符串值是否为日期?
Not really, seeing as it could be in an arbitrary format.
不是真的,因为它可能是任意格式。
If at all possible, I would tend to leave parsing to the magic of strtotime()
. If it manages to create a valid date, fine. If it doesn't, you'll receive false
.
如果可能的话,我倾向于将解析留给strtotime()
. 如果它设法创建一个有效的日期,那很好。如果没有,您将收到false
.
Be prepared for the possibility of false positives, though, because strtotime()
parses even things like "Last Friday".
但是,请为误报的可能性做好准备,因为strtotime()
甚至会解析诸如“上周五”之类的内容。
If strtotime()
is too liberal for you, you could consider building a collection of date formats you want to accept, and run PHP 5.3's DateTime:createFromFormat
using every one of the formats on every date.
如果strtotime()
对你来说太自由了,你可以考虑构建一个你想要接受的日期格式的集合,并DateTime:createFromFormat
在每个日期使用每一种格式运行 PHP 5.3 。
Something like (untested)
类似的东西(未经测试)
$formats = array("d.m.Y", "d/m/Y", "Ymd"); // and so on.....
$dates = array(1,2,3,"4","11/12/2009","22/12/2000",true,false);
foreach ($dates as $input)
{
foreach ($formats as $format)
{
echo "Applying format $format on date $input...<br>";
$date = DateTime::createFromFormat($format, $input);
if ($date == false)
echo "Failed<br>";
else
echo "Success<br>";
}
}
回答by Dave
The problem with Pekka's script is that the date '2011-30-30' is also considered valid. This is the modified version.
Pekka 脚本的问题在于日期“2011-30-30”也被认为是有效的。这是修改后的版本。
$formats = array("d.m.Y", "d/m/Y", "Ymd"); // and so on.....
$dates = array(1,2,3,"4","11/12/2009","22/12/2000",true,false);
foreach ($dates as $input)
{
foreach ($formats as $format)
{
echo "Applying format $format on date $input...<br>";
$date = DateTime::createFromFormat($format, $input);
if ($date == false || !(date_format($date,$format) == $input) )
echo "Failed<br>";
else
echo "Success<br>";
}
}
回答by iateadonut
Extrapolating from http://au1.php.net/checkdate#113205; just change the $formats array to all the formats you want to check.
从http://au1.php.net/checkdate#113205推断;只需将 $formats 数组更改为您要检查的所有格式。
public function convertDate($value) {
$formats = ['M d, Y', 'Y-m-d'];
foreach($formats as $f) {
$d = DateTime::createFromFormat($f, $value);
$is_date = $d && $d->format($f) === $value;
if ( true == $is_date ) break;
}
return $is_date;
}