1

I have a situation where I need to create a Datetime object from a string. The issue comes when I don't have consistency in the pattern of the string representing the date. These are examples of my data:

07/17/2012
2013/03/11
17/05/2015
17/17/2015

As you can see, the last one is invalid no matter what, because there is no 17 month, but the first 3 are valid depending on the month position(and of course, the year)

My question: is there any way(pretty sure through regex) to make a function with the date string as parameter that return the datetime object. If the string is not valid, return: 1/1/1970...

3
  • 1
    What about ambiguous dates like 4/6/2015 ? Commented Jun 4, 2015 at 14:42
  • You have to think for a solution for how your program can differentiate the different formats. Commented Jun 4, 2015 at 14:45
  • For example: count the number of characters before the first '/'. If it is 4 you know its the year. Commented Jun 4, 2015 at 14:46

3 Answers 3

2

The issue comes when I don't have consistency in the pattern of the string representing the date

for this purpose, php offers strtotime(): http://php.net/manual/en/function.strtotime.php

Example usage:

   $str1 = "2015-06-04 16:00";
   $str2 = "06/04/2015 4 pm";
   $str3 = "04.06.2015 16:00";

   $actualDate = date("Y-m-d H:i:s", strtotime($str1));
   echo $actualDate."<br />";
   $actualDate = date("Y-m-d H:i:s", strtotime($str2));
   echo $actualDate."<br />";
   $actualDate = date("Y-m-d H:i:s", strtotime($str3));
   echo $actualDate."<br />";

   //all will produce "2015-06-04 16:00:00"

as a bonus, strtotime also supports "insane" expressions like

$actualDate = date("Y-m-d H:i:s", strtotime("06/04/2015 + 1 day - 8 hours")); 
echo $actualDate."<br />";
// "2015-06-04 16:00:00"

and many more such as "monday this week", "tuesday next week", "first monday of january 2038" and the like.

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

Comments

1

You can try to create DateTime Object with your string values. It will throw exception if the date format is not valid, then you can just catch it and return your 1/1/1971

try {
    $dateTime = new DateTime('17/17/2015');
    return $dateTime;
} catch (Exception $e) {
   return '1/1/1971';
}

Comments

0

You can use DateTime.

$myDate = '17/17/2015';

$date = DateTime::createFromFormat('d/m/Y', $myDate);

if (DateTime::getLastErrors()['warning_count'] >= 1) {
    // Invalid
} else {
    // Valid date
}

3 Comments

The problem is I can't asure this format: 'd/m/Y' in createFromFormat('d/m/Y', $myDate) Take a look to the data examples
You could use strtotime, then?
If you allow completely custom dates you will always encounter problems. E.g., 1/2/15 might be 1st February or the 2nd January.

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.