0

Anyone can see what is wrong here? I can`t :(

Example date: day 10, month 02, year 2017

In my DB it inserts: 2017-10-02

DB is set to DATE

Jquery Datepicker:

    $('.datepicker').daterangepicker({ 
    singleDatePicker: true,
    locale: {
        format: 'DD/MM/YYYY'
    }
});

Datepicker outputs to HTML: 10/02/2017

PHP:

$Newdate = date('Y-m-d', strtotime(str_replace('-', '/', $DateTaken)));

Why is the day and month switched?

1 Answers1

0

You have to change the date format on date() function:

$Newdate = date('d-m-Y', strtotime(str_replace('-', '/', $DateTaken)));

Documentaion: http://php.net/manual/en/function.date.php

angelos_lex
  • 1,593
  • 16
  • 24
  • Have read the doc, but what you suggest makes the date inserted as 0000-00-00. The date is correct when just displaying the input of user, but convert that date to a date mysql understand date"Y-m-d" and even still it switch the month and day ?!?! – KommerSnart Feb 05 '17 at 01:22
  • check this here: http://stackoverflow.com/questions/8338031/mysql-setup-the-format-of-datetime-to-dd-mm-yyyy-hhmmss-when-creating-a-tab ... "MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format." This is from mysql site. You can store only this type, but you can use one of the many time format functions to change it, when you need to display it. – angelos_lex Feb 05 '17 at 01:49
  • Ok, changed the table from DATE to DATETIME and tried again with the new date format, same result. But if i changed from date("Y-m-d") to date("Y-d-m") it worked! Date was saved correctly into db as the user typed in. day 10 month 2 year 2017 result in db = 2017-02-10 00:00:00) :D – KommerSnart Feb 05 '17 at 09:40