如何确定值是PHP中的日期 [英] How to determine if value is a date in PHP

查看:121
本文介绍了如何确定值是PHP中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP中的值数组。这些值中的一些可能包含各种字符串格式的日期。



我需要将多种格式的日期转换为数字等价物(Unix时间戳)。问题是能够确定字符串是否为日期。



使用

  if(($ timestamp = strtotime($ str))=== false)

将检查字符串的有效日期,但如何确定该值是否甚至被验证为日期?



示例: / p>

  $ x = {1,2,3,4,11/12/2009,22/12 / 2000,true,false}; 

foreach($ x as $ value)
{

if(is_bool($ value))

if(is_string($值)

if(is_numeric($ value))

if(is_date($ value))?

...

}

简单来说,是否有一个简单的方法来检查字符串值是否为日期?

解决方案


很简单,有一个简单的方法来检查字符串值是否是日期?


不是真的,看到它可能在一个任意格式。



如果可能的话,我会倾向于解析 strtotime()的魔法。如果它设法创建有效的日期,罚款。如果没有,您将收到 false



尽可能准备假阳性的可能性,因为 strtotime()解析甚至像上周五。



如果 strtotime()对你来说太开放了,你可以考虑构建一个你想要的日期格式的集合接受并运行PHP 5.3的 DateTime:createFromFormat 在每个日期使用每种格式。



某些东西(未经测试)

  $ formats = array dmY,d / m / Y,Ymd); //等等..... 
$ dates = array(1,2,3,4,11/12/2009,22/12/2000,true,false);

foreach($ dates as $ input)
{
foreach($ formats as $ format)
{
echo在日期中应用格式$格式$ input ...< br>;

$ date = DateTime :: createFromFormat($ format,$ input);
if($ date == false)
echoFailed< br>;
else
echoSuccess< br>;
}
}


I am working with arrays of values in PHP. Some of these values may include a date in various string formats.

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.

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?

解决方案

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.

Be prepared for the possibility of false positives, though, because strtotime() parses even things like "Last Friday".

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.

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>";
    }
 }

这篇关于如何确定值是PHP中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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