convert date data into mysql date format

17,010

Yes, you can convert the date with strtotime();

$dateToday = date("d-m-Y");
$newDate = date("Y-m-d", strtotime($dateToday));

OUTPUT: 2012-12-27

And then you can store data to your database.

When you have to recover the date you can reverse this operation like this:

$dateFromDatabase = "2012-12-27";
$reverseDate = date("d-m-Y", strtotime($dateFromDatabase));

OUTPUT: 27-12-2012

(corrected "Y-m-d" to "d-m-Y" in 2nd date call)

Share:
17,010
ramadani
Author by

ramadani

Updated on June 16, 2022

Comments

  • ramadani
    ramadani almost 2 years

    I want to convert the data on which I have the format

    $dateToday = date("d-m-Y");
    

    so the value of $dateToday is 27-12-2012

    Then I want to save it to the database with the mysql data type date. How to keep the value of 27-12-2012 it can be stored in the mysql database with the format 2012-12-27? Help me please. Thank you

  • ramadani
    ramadani over 11 years
    yes, that's work. thank you very much friends. it's very help me :)
  • Samiul
    Samiul about 9 years
    it will not work for if i input 04-03-2015 in US format