我从一个API接收到一个日期字符串,它的格式是yyyy-mm-dd。

我目前正在使用一个正则表达式来验证字符串格式,这是可以工作的,但我可以看到一些情况下,根据字符串,它可能是一个正确的格式,但实际上是一个无效的日期。例如,2013年13月1日。

在PHP中是否有更好的方法来获取一个字符串,如2013-13-01,并判断它是否是yyyy-mm-dd格式的有效日期?


当前回答

试一试:

$date = "2017-10-01";


function date_checker($input,$devider){
  $output = false;

  $input = explode($devider, $input);
  $year = $input[0];
  $month = $input[1];
  $day = $input[2];

  if (is_numeric($year) && is_numeric($month) && is_numeric($day)) {
    if (strlen($year) == 4 && strlen($month) == 2 && strlen($day) == 2) {
      $output = true;
    }
  }
  return $output;
}

if (date_checker($date, '-')) {
  echo "The function is working";
}else {
  echo "The function isNOT working";
}

其他回答

正则表达式的解决方案

function verify_date($date){
  /* correct format = "2012-09-15 11:23:32" or "2012-09-15"*/
  if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])( (0[0-9]|[1-2][0-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9]))?$/",$date)) {
    return true;
  } else {
    die("Wrong date format: it should be '2012-09-15 11:23:32' or '2012-09-15', date received is: ".$date);
  }
}

判断是否有字符串是日期

function checkIsAValidDate($myDateString){
    return (bool)strtotime($myDateString);
}

试着让我知道这对我有用

$date = \DateTime::createFromFormat('d/m/Y', $dataRowValue);
if (!empty($date)) {
//Your logic
}else{
//Error
}

如果您传递任何alpha或字母数字值,它将返回给您空值

你也可以解析月份日期和年份的日期,然后你可以使用PHP函数checkdate(),你可以在这里读到:http://php.net/manual/en/function.checkdate.php

你也可以试试这个:

$date="2013-13-01";

if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date))
    {
        echo 'Date is valid';
    }else{
        echo 'Date is invalid';
    }

我有这样的东西,即使使用PHP,我也喜欢寻找函数式解决方案。例如,@migli给出的答案非常好,非常灵活和优雅。

但它有一个问题:如果需要用相同的格式验证大量DateTime字符串怎么办?你将不得不到处重复这种格式,这违背了DRY原则。我们可以把格式放在一个常量中,但是,我们仍然必须把这个常量作为参数传递给每个函数调用。

但是不要再害怕了!我们可以用咖喱来拯救我们!PHP并没有让这个任务变得愉快,但仍然可以用PHP实现curry:

<?php
function validateDateTime($format)
{
    return function($dateStr) use ($format) {
        $date = DateTime::createFromFormat($format, $dateStr);
        return $date && $date->format($format) === $dateStr;
    };
}

那么,我们刚刚做了什么?基本上,我们将函数体包装在一个匿名函数中,并返回这样的函数。我们可以像这样调用验证函数:

validateDateTime('Y-m-d H:i:s')('2017-02-06 17:07:11'); // true

是啊,差别不大…但真正的力量来自于部分应用的函数,这是通过咖喱实现的:

// Get a partially applied function
$validate = validateDateTime('Y-m-d H:i:s');

// Now you can use it everywhere, without repeating the format!
$validate('2017-02-06 17:09:31'); // true
$validate('1999-03-31 07:07:07'); // true
$validate('13-2-4 3:2:45'); // false

函数式编程!