Converting a Date to MySql Formatted Date
March 12, 2009Converting a Date to mySql Formatted Date
[REPUBLISHED FROM OLD SITE]
This is a php coded function provided to allow people to convert dates from standard input format to mysql storage format./** * function dateconvert * * dateconvert is a handy function to take one of the aches and pains out of mysql * by converting data from a variable (posted from a form or just stored) * into a format mysql will be able to store and converting the * database date back into the british standard of date month year. * The Script accepts day.month.year or day/month/year or day-month-year. * example: * * * $date = "19.12.2005"; * $date = dateconvert($date, 1); * echo $date; // Would echo 2005-12-19 which is the format stored by mysql * * *<?php * $date = $row['date']; //your mysql date * $realdate = dateconvert($date,2); * echo $realdate; // would display 19/12/2005 *?> * * * @author Chris McKee - pcdevils@gmail.com * * @param string $date - Date to be converted * @param string $func - which function is to be used (1 for input to mysql, 2 for output from mysql) */ function dateconvert($date,$func) { if ($func == 1){ //insert conversion list($day, $month, $year) = split('[/.-]', $date); $date = "$year-$month-$day"; return $date; } if ($func == 2){ //output conversion list($year, $month, $day) = split('[-.]', $date); $date = "$day/$month/$year"; return $date; } }
*** NOTE ***
This is a bit old (PHP4) but feel free to use. Works in PHP5 too.

One Response to “ Converting a Date to MySql Formatted Date ”
that works fine with php 5 and saved me a lot of time. big thanks.