Monday, March 28, 2011

function to convert in order to save it to mysql

function to convert in order to save it to mysql

Here's one simple function which is often used to convert in order to save it to mysql and opposite, that is to display the date in the format we want, at this function that we want is dd / mm / yyyy (day / month / year).


// FUNGSI DATE CONVERT
function jin_date_sql($date){
$exp = explode('/',$date);
if(count($exp) == 3) {
$date = $exp[2].'-'.$exp[1].'-'.$exp[0];
}
return $date;
}

function jin_date_str($date){
$exp = explode('-',$date);
if(count($exp) == 3) {
$date = $exp[2].'/'.$exp[1].'/'.$exp[0];
}
return $date;
}


How to use are as follows.:
Convert from date DD / MM / YYYY to YYYY-MM-DD to insert into mysql database


$data_tanggal_form = "23/02/2009"; // DD/MM/YYYY
$data_tanggal_mysql = jin_date_sql($data_tanggal_form); // hasilnya: 2009-01-01 = YYYY-MM-DD


And the reverse, ie want to display.
Convert from date YYYY-MM-DD to DD / MM / YYYY to perform capture from mysql database

$data_tanggal_db = "2009-01-01"; // YYYY-MM-DD
$data_tanggal_tampil = jin_date_str($data_tanggal_db); // hasilnya: 23/02/2009 = DD/MM/YYYY

No comments:

Post a Comment

 
THANK YOU FOR VISITING