3

I have the following string:

08/07/2012 04:17:18

How can I check if it is a date data type by using PHP? For example, if I was checking for integer, I would do this: if(is_int($checkVar). How can I do the same for date format of this kind?

5
  • 3
    Define "date"? Do you mean "can be parsed" or "is theoretically some kind of representation of a date even if that date is in a really odd format"? Commented Nov 27, 2017 at 21:38
  • 1
    you can try "instanceof DateTime" ? Commented Nov 27, 2017 at 21:39
  • @tadman Generally, all my dates are in that format. So I would like to get a boolean with true or false when I run it through checkings. Commented Nov 27, 2017 at 21:39
  • 1
    You can always write a regular expression, or if you're fairly confident they are dates, just jam it into the parser and see if you get back something sensible. Presumably dates from the year 0 or 9999 are invalid, for example. Commented Nov 27, 2017 at 21:40
  • Are these just strings from some file you are reading? Commented Nov 27, 2017 at 21:44

3 Answers 3

6
date_parse($theString)

will return an array of the following format

Array
(
    [year] => 2006
    [month] => 12
    [day] => 12
    [hour] => 10
    [minute] => 0
    [second] => 0
    [fraction] => 0.5
    [warning_count] => 0
    [warnings] => Array()
    [error_count] => 0
    [errors] => Array()
    [is_localtime] => 
)

or false if it can't find a date. if that's enough for your needs, then use this!

Sign up to request clarification or add additional context in comments.

Comments

6
if (strtotime($date) !== false)

strtotime() returns numeric if the date is valid. Note that strtotime() and date_parse() also work on strings, so if you're validating for a DB and don't want stuff like

March 1st 2011

to work then you can't use these functions. For a mysql date I usually do

$date = date('Y-m-d',strtotime($date));

which formats any date format into the format needed. If the original $date was not in date format, the resulting $date will be false.

2 Comments

Returns '1970-01-01' if the date isn't valid, which may not be helpful
Scroll up and you'll see the strtotime() check first.
1

You're not really checking if a variable is of type date. You have a string in a very specific format and want to make sure that it conforms with that format.

One way to do that, is with a regular expression (preg_match()). It's fairly simple to get something that's reasonably correct, but it might be hard if you also care about leap years, daylight savings time jumps, or if you're not ok with someone specifying February 30th.

The DateTime object can help you a little bit with that. You could parse out the inidivual components of your date, add it to the DateTime constructor and then see if the DateTime constructor comes up with something similar to the original input.

For example, I'm fairly sure that DateTime will convert April 31st (which doesn't exist) to May 1st manually. So you could check if your input month (4) matches the month DateTime is using (5). If all of those match up, and there was no error, it was a valid string in your format.

2 Comments

Why doesn't March 31st exist?
@Danial I made a mistake there. Fixed now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.