phpzend-frameworkzend-date

formatting date and time in php for sql insertion


I have a date, and a time, which I parse from a csv file, $date being a string, and it holds the value '18-06-2013', and the $time string var holds the value '06.00'. I need it formatted by php so that I can insert it into my mysql database in the format 2013/06/18 and 06:00:00. How do I do it? Right now, it inserts some zero junk values like 0000-00-00 for date and 00:00:06 for time.


Solution

  • You could split it on the '-' token and then join it back in the reverse order (assuming your input format is DD-MM-YYYY, which it seems to be)

    $date = implode('-', array_reverse(explode('-', $date, 3));
    $time = str_replace('.', ':', $time) . ':00';
    $datetime = "$date $time";