0

I am posting data with dates in this format:

06/11/2015

i then use the below function to change the date format to insert into a database:

ChangeDateFormat('06/11/2015', 'Y-m-d');

date_default_timezone_set('Europe/London');
if(!function_exists("ChangeDateFormat")) {
    function ChangeDateFormat($date = '', $format = '') {
        if($date == '' or $date == '0000-00-00' or $date == '0000-00-00 00:00:00') {
            return '-';
        } else {
            return date($format, strtotime($date));
        }
    }
}

but its inserting into the following format:

2015-06-11 whereas it should be 2015-11-06

charlie
  • 415
  • 4
  • 35
  • 83
  • 1
    When using `strtotime()`, a date string containing `/` will be treated as US format (`m/d/Y`); A date string containing `-` will be treated as UK/European format (`d/m/Y`); as documented in the [PHP Docs](http://www.php.net/manual/en/datetime.formats.date.php) – Mark Baker Dec 10 '15 at 18:42
  • That's an american format but you're trying to use it as European. That won't work. – John Conde Dec 10 '15 at 18:42
  • PHP tries to guess what date format was meant. This cannot always be correct, since that notation is vague. That is a huge problem with those old fashioned notations still in use in the anglophile world. Same issue with temperatures, times, weights, sizes, ... – arkascha Dec 10 '15 at 18:43

1 Answers1

1

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. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible. link

Hungry Mind
  • 226
  • 1
  • 4
  • 12