4

My SQL server uses the mm/dd/yyyy date format, but the date picker that I have implemented using jQuery gives the date format as dd/mm/yyyy.

So I coded this to check if the given input is in the format of mm/dd/yyyy, but it evaluates to true no matter which format the date input is given in. PHP code is,

$Temp = DateTime::createFromFormat('d/m/Y', $StartsOn);
    if($Temp) 
        $Temp->format('m/d/Y');

I need to convert to mm/dd/yyyy format only if the input is in dd/mm/yyyy. So please tell me what is the logical error that I have made in my code.

16
  • Why dont you explode mm/dd/yyyy and put it then together?
    – B001ᛦ
    Commented Jun 15, 2015 at 12:53
  • Why the sql-server tag? Your question is just php related.
    – Ionic
    Commented Jun 15, 2015 at 12:54
  • 1
    What is the definition of the date column, as it's unusual for it to be mm/dd/yyyy? Normally I'd expect to see it as yyyy-mm-dd.
    – DanielM
    Commented Jun 15, 2015 at 12:55
  • 1
    Just an FYI, there is no way to properly be able to tell the difference between mm/dd/yyyy and dd/mm/yyyy (which is why you should never use the former, sorry US). For example is 02/03/2015 the 2nd of March or 3rd of February. Reference: xkcd.com/1179
    – DanielM
    Commented Jun 15, 2015 at 12:58
  • 1
    with xx-xx-xxxx or xx/xx/xxxx, PHP treats a - separator as US format, a / separator as European format - as documented, php.net/manual/en/datetime.formats.date.php
    – Mark Baker
    Commented Jun 15, 2015 at 13:07

3 Answers 3

4

it's impossible to reliably check if a date is in dd/mm/yyyy or mm/dd/yyyy format. just think about a date like "May, 7th".
this would be 07/05/2015 or 05/07/2015 depending on the format. so if you just got the date-string with no additional information you can't tell if for example 05/07/2015 is May, 7th or July, 5th.

I am sorry but there is no logical solution to your problem.

0
2

From the PHP manual on strtotime

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

The reason passing the date to SQL server works as mm/dd/yyyy is because of the separator. Where possible it is always best to pass as YYYY-MM-DD as per ISO 8601 which was created for exactly this purpose. To fix your problem the best bet is to change the jQuery plugin configuration to output data in that format (if that's not possible, string replace / with - where it's coming from the jQuery plugin. This will avoid future complications by writing code to fix the date format.

You will no be able to tell the difference between mm/dd/yyyy and dd/mm/yyyy when you don't know where it's come from.

0
0

You can use a modified version of this function from PHP.net. It uses the DateTime class:

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

if(validateDate($StartsOn)){
//do job
}

function was copied from this answer or php.net

6
  • this would only work for dates where the day of the month is bigger than 12. otherwise you would still not know if for example 08/09/2015 means "September, 8th" or "August, 9th".
    – low_rents
    Commented Jun 15, 2015 at 13:05
  • @low_rents we are validating if the date format is correct not the actual date is correct. Commented Jun 15, 2015 at 13:14
  • @low_rents have you a got a change to look up the title of question How to check format of Date in PHP? Commented Jun 15, 2015 at 13:16
  • 1
    yes - s/he is asking how to check which format the date uses - not how to validate it.
    – low_rents
    Commented Jun 15, 2015 at 13:18
  • Why should the OP "use this function"? Please add an explanation of what you did and why you did it that way, not only for the OP but for future visitors to SO. Commented Jun 15, 2015 at 14:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.