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.

Ionic
  • 3,884
  • 1
  • 12
  • 33
SoundStage
  • 802
  • 2
  • 9
  • 27
  • Why dont you explode mm/dd/yyyy and put it then together? – B001ᛦ Jun 15 '15 at 12:53
  • Why the sql-server tag? Your question is just php related. – Ionic Jun 15 '15 at 12:54
  • which date picker you are using? – Vivek Singh Jun 15 '15 at 12:55
  • It can be impossible to determine if some dates are dd/mm or mm/dd. If you use proper `date` fields in MySQL then you'll be fine. – Geoff Atkins Jun 15 '15 at 12:55
  • @bub Because I am trying to make use of the `DateTime::createFromFormat()` function as I feel using explode is a brute force way of doing what I am aiming for. – SoundStage Jun 15 '15 at 12:55
  • 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 Jun 15 '15 at 12:55
  • @GeoffAtkins I am not using MySQL, I am working with SQL Server – SoundStage Jun 15 '15 at 12:57
  • @Ionic I am new to this site, so my bad that I added all possible tags. – SoundStage Jun 15 '15 at 12:58
  • 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: https://xkcd.com/1179/ – DanielM Jun 15 '15 at 12:58
  • @DanielM Just the default date column in SQL server, I did not specify any format. – SoundStage Jun 15 '15 at 13:00
  • @Rai Microsoft SQL server uses YYYY-MM-DD Reference: https://msdn.microsoft.com/en-gb/library/ms186724.aspx – DanielM Jun 15 '15 at 13:01
  • @DanielM Yes, you are correct. But passing the date format in `mm/dd/yyyy` seems to work from PHP, which I have no idea how. – SoundStage Jun 15 '15 at 13:03
  • OK, I know why so I have an answer for you. Writing it up now. – DanielM Jun 15 '15 at 13:04
  • 1
    with `xx-xx-xxxx` or `xx/xx/xxxx`, PHP treats a `-` separator as US format, a `/` separator as European format - as documented, http://php.net/manual/en/datetime.formats.date.php – Mark Baker Jun 15 '15 at 13:07
  • how can you not know which format is a given date string? You either got it from the database, or from a datepicker. In either case, you know which format it is. – Félix Adriyel Gagnon-Grenier Jun 16 '15 at 04:38
  • @FélixGagnon-Grenier In the application that I am developing, the date from the DB is converted and displayed in the `dd-mm-yy` format, but datepicker works in `mm-dd-yy` format. So I needed to find the format after the user changes the date by selecting a newer date through datepicker. – SoundStage Jun 16 '15 at 09:41

3 Answers3

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.

low_rents
  • 4,481
  • 3
  • 27
  • 55
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.

DanielM
  • 6,380
  • 2
  • 38
  • 57
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

Community
  • 1
  • 1
Faiz Rasool
  • 1,379
  • 1
  • 12
  • 20
  • 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 Jun 15 '15 at 13:05
  • @low_rents we are validating if the date format is correct not the actual date is correct. – Faiz Rasool Jun 15 '15 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?** – Faiz Rasool Jun 15 '15 at 13:16
  • 1
    yes - s/he is asking how to check which format the date uses - not how to validate it. – low_rents Jun 15 '15 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. – Jay Blanchard Jun 15 '15 at 14:52
  • I am not looking to validate the date format, I need to check which date format is being used explicitly. – SoundStage Jun 16 '15 at 05:09