100

I am trying to write a function to determine if a string is a date/time using PHP. Basically a valid date/time would look like:

 2012-06-14 01:46:28 

Obviously though its completely dynamic any of the values can change, but it should always be in form of XXXX-XX-XX XX:XX:XX, how can I write a regular expression to check for this pattern and return true if matched.

1

17 Answers 17

172

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 } 
Sign up to request clarification or add additional context in comments.

10 Comments

This is not a solution. I have DateTime::createFromFormat('m/d/Y', '10/38/2013') and that produces a valid DateTime object and not false. The date is converted to object(DateTime)#39 (3) { ["date"]=> string(19) "2013-11-07 23:45:55" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }
Yikes. I love how these things are never pointed out in the documentation ... You don't happen to have found an alternative? (Edit: Date formats documents that you can over-/underflow months and days, but doesn't give a hint what to do when you don't want that behaviour.
Okay well there's no solution or setting to override this in PHP. I noted a workaround in the answers stackoverflow.com/a/19666600/486863
This solution indeed is not working ok for dates like the one specified by cj5 . Which can be very frustrating. I used as well this solution stackoverflow.com/questions/10120643/…
It's my format Tue Oct 30 05:51:41 GMT+05:30 2018 how can we check it?, is there any common way of doing?
|
92

Easiest way to check if a string is a date:

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

8 Comments

I can't see why this would not be the accepted answer. Here is the PHP manual that specifically says this will return FALSE if anything but a date string: php.net/manual/en/function.strtotime.php
This is the best, elegant, simple and effective method I've seen so far. Thank You Yogesh Mistry
Does not work for strtotime('a string'); -> returns current time.
give incorrect result is string starts with +
strtotime("2021-02-31 08:00:00") will return int(1614787200) which evaluates to TRUE but it isn't a valid datetime string as OP asked for
|
57

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

2 Comments

This should be the accepted answer. It was the only solution that could properly handle leap years "2023-02-29" is false and "2020-02-29" is true. FYI, depending on your use case, you can optionally remove the "H:i:s" if you only care about date (and not time of day)
for anyone finding this like I did: careful, if your datetime strings include timezones, you have to make sure to include that in the date you create too. And if they're not always the same this can be annoying.
35

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 

4 Comments

var_dump(isDate('now')); //true var_dump(isDate('tomorrow')); //true var_dump(isDate('yesterday')); //true
Beware integers! isDate('123456') == true and isDate(123456) == true
var_dump(isDate("R")) = true. probably not what's intended. I think it matches a time zone or something.
this is great if you know what you're passing into it. In my case I do
12

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

5 Comments

What's date_parse and checkdate?
These are PHP given functions: php.net/manual/en/function.date-parse.php php.net/manual/en/function.checkdate.php 'date_parse' creates an array from the given string, by trying to identify possible date parts. 'checkdate' validates the date if you pass your variables in integer format as ($month, $day, $year). So the real question here is how well does the date_parse work. In my tests it worked better, than the other functions suggested by users on this page.
Works well. Needed a method to decide if I was going to try and parse the date, or just display it as plain text. Worked well.
One caveat though. The date format you enter has to be 'culturally appropriate' to prevent the date_parse function from showing false positive errors. The function presumes that slash-separated date string is in North American format and a dash-separated date string is a European-style date. Therefore, a string like 30/9/2022 will show errors, but a 30-9-2022 string will pass with no problems. Interesting.
This is best answer for check string is date(/time) from unknown format but it must be valid and acceptable by PHP.
5

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

1 Comment

^(\d{4}(?:\-\d{2}){2} \d{2}(?:\:\d{2}){2})$
5
if (strtotime($date) > strtotime(0)) { echo 'it is a date' } 

1 Comment

strtotime('a string') > strtotime(0) will return true
4

A simple solution is:

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

Comments

3

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.

Comments

2

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 } 

Comments

0

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 */ 

Comments

0

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

Comments

0

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); 

Comments

0
 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

Comments

0

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

Comments

0

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

Comments

-2

strtotime? Lists? Regular expressions?

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.