函数检查字符串是否为日期

我正在尝试使用 PHP 编写一个函数来确定一个字符串是否是一个日期/时间。基本上,有效的日期/时间应该是这样的:

 2012-06-14 01:46:28

显然,虽然它的完全动态的任何值都可以改变,但它应该始终是 XXXX-XX-XX XX:XX:XX的形式,我如何写一个正则表达式来检查这个模式,并返回真,如果匹配。

160537 次浏览

I use this function as a parameter to the PHP filter_var function.

  • It checks for dates in yyyy-mm-dd hh:mm:ss format
  • It rejects dates that match the pattern but still invalid (e.g. Apr 31)

function filter_mydate($s) {
if (preg_match('@^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$@', $s, $m) == false) {
return false;
}
if (checkdate($m[2], $m[3], $m[1]) == false || $m[4] >= 24 || $m[5] >= 60 || $m[6] >= 60) {
return false;
}
return $s;
}

If that's your whole string, then just try parsing it:

if (DateTime::createFromFormat('Y-m-d H:i:s', $myString) !== false) {
// it's a date
}

I wouldn't use a Regex for this, but rather just split the string and check that the date is valid:

list($year, $month, $day, $hour, $minute, $second) = preg_split('%( |-|:)%', $mydatestring);
if(!checkdate($month, $day, $year)) {
/* print error */
}
/* check $hour, $minute and $second etc */

Here's a different approach without using a regex:

function check_your_datetime($x) {
return (date('Y-m-d H:i:s', strtotime($x)) == $x);
}

strtotime? Lists? Regular expressions?

What's wrong with PHP's native DateTime object?

http://www.php.net/manual/en/datetime.construct.php

If your heart is set on using regEx then txt2re.com is always a good resource:

<?php


$txt='2012-06-14 01:46:28';
$re1='((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))';    # Time Stamp 1


if ($c=preg_match_all ("/".$re1."/is", $txt, $matches))
{
$timestamp1=$matches[1][0];
print "($timestamp1) \n";
}


?>

Although this has an accepted answer, it is not going to effectively work in all cases. For example, I test date validation on a form field I have using the date "10/38/2013", and I got a valid DateObject returned, but the date was what PHP call "overflowed", so that "10/38/2013" becomes "11/07/2013". Makes sense, but should we just accept the reformed date, or force users to input the correct date? For those of us who are form validation nazis, We can use this dirty fix: https://stackoverflow.com/a/10120725/486863 and just return false when the object throws this warning.

The other workaround would be to match the string date to the formatted one, and compare the two for equal value. This seems just as messy. Oh well. Such is the nature of PHP dev.

 function validateDate($date, $format = 'Y-m-d H:i:s')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}

function was copied from this answer or php.net

If you have PHP 5.2 Joey's answer won't work. You need to extend PHP's DateTime class:

class ExDateTime extends DateTime{
public static function createFromFormat($frmt,$time,$timezone=null){
$v = explode('.', phpversion());
if(!$timezone) $timezone = new DateTimeZone(date_default_timezone_get());
if(((int)$v[0]>=5&&(int)$v[1]>=2&&(int)$v[2]>17)){
return parent::createFromFormat($frmt,$time,$timezone);
}
return new DateTime(date($frmt, strtotime($time)), $timezone);
}
}

and than you can use this class without problems:

ExDateTime::createFromFormat('d.m.Y G:i',$timevar);

In case you don't know the date format:

/**
* Check if the value is a valid date
*
* @param mixed $value
*
* @return boolean
*/
function isDate($value)
{
if (!$value) {
return false;
}


try {
new \DateTime($value);
return true;
} catch (\Exception $e) {
return false;
}
}


var_dump(isDate('2017-01-06')); // true
var_dump(isDate('2017-13-06')); // false
var_dump(isDate('2017-02-06T04:20:33')); // true
var_dump(isDate('2017/02/06')); // true
var_dump(isDate('3.6. 2017')); // true
var_dump(isDate(null)); // false
var_dump(isDate(true)); // false
var_dump(isDate(false)); // false
var_dump(isDate('')); // false
var_dump(isDate(45)); // false

Easiest way to check if a string is a date:

if(strtotime($date_string)){
// it's in date format
}

In my project this seems to work:

function isDate($value) {
if (!$value) {
return false;
} else {
$date = date_parse($value);
if($date['error_count'] == 0 && $date['warning_count'] == 0){
return checkdate($date['month'], $date['day'], $date['year']);
} else {
return false;
}
}
}
if (strtotime($date) > strtotime(0)) {
echo 'it is a date'
}

I found my answer here https://stackoverflow.com/a/19271434/1363220, bassically

$d = DateTime::createFromFormat($format, $date);
// The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
if($d && $d->format($format) === $date) {
//it's a proper date!
}
else {
//it's not a proper date
}

This solves for me, but also presents various other problems I think.

function validateTimeString($datetime, $format = "Y-m-d H:i:s"){
return ($datetime == date($format, strtotime($datetime)));
}

When I work with unconventional APIs, I sometimes get a bit of a messy return instead of a well defined date format. So I use a rather inelegant class and I readily admit that it is brutal and unconventional in principle but it does me good sometimes ^^.

class DateHelper
{
private const DATE_FORMATS = [
DATE_ATOM,
DATE_COOKIE,
DATE_RFC822,
DATE_RFC850,
DATE_RSS,
DATE_W3C,
"Y-m-d\TH:i:s.u",
'Y-m-d\TH:i:s',
"Y-m-d'T'H:i:s.SSS'Z'",
"Y-m-d\TH:i:s.uP",
"Y-m-d\TH:i:sP",
"d/m/Y H:i:s",
];


/**
* @param string $inputStringDate
* @return DateTime|null
*/
public static function createDateFromUnknownFormat(string $inputStringDate): ?DateTime
{
$inputStringDate = str_replace('/', '-', $inputStringDate);
preg_match('/^(\d{4})\-(\d{2})-(\d{2})$/', $inputStringDate, $result);
if (!empty($result)) {
return DateTime::createFromFormat('Y-m-d', $inputStringDate);
}
preg_match('/^(\d{2})\-(\d{2})-(\d{4})$/', $inputStringDate, $result);
if (!empty($result)) {
return DateTime::createFromFormat('d-m-Y', $inputStringDate);
}
foreach (self::DATE_FORMATS as $dateFormat) {
if ($dateObject = DateTime::createFromFormat($dateFormat, $inputStringDate)) {
return $dateObject;
}
}
return null;
}
}

A simple solution is:

echo is_numeric( strtotime( $string ) ) ? 'Yes' : 'No';