Jump to content

Recommended Posts

Dear all,

 

I'm using MySQL database, the problem is I've setup that user input the Date format in dd/mm/yyyy (example: 14may2007) and Time (example: 12:00pm). How do I convert it to Date & Time format in database format before insert into database? Sorry, noob here. Any advice or suggestion are most welcome. Thank you, regards.

Link to comment
https://forums.phpfreaks.com/topic/51272-date-time-format-in-database/
Share on other sites

<?php
$date_format = "23feb2007";
$time_format = "12:00pm";

//-------------------------------------------------- End for Time

$time = substr($time_format,0,5);
$format = substr($time_format,5,2);
if($format == 'pm'){
   if($time == "12:00"){
   		$new_formated_time = "12:00"; 
   }else{
   $time_arr = explode(":",$time);
   $t_time = 12 +  $time_arr[0];
   $new_formated_time =  $t_time .":".$time_arr[1];
    }
}else{
if($time == "12:00"){
	$new_formated_time = "00:00"; 
}else{
   $new_formated_time =   $time;
}
}

$time_l = explode(":",$new_formated_time);
$hour_l = $time_l[0];
$mnt_l = $time_l[1];
// ------------------------------------------------ For Date
$date_l = substr($date_format,0,2);
$month_l = substr($date_format,2,3);
$year_l = substr($date_format,5,4);
$months = array("jan"=>"01", "feb"=>"02", "mar"=>"03", "apr"=>"04", "may"=>"05", "jun"=>"06","jul"=>"07", 
		"aug"=>"08", "sep"=>"09", "oct"=>"10", "nov"=>"11", "dec"=>"12");


$final_date = date("Y-m-d H:i:s",mktime($hour_l,$mnt_l,0,$months[$month_l],$date_l,$year_l));

//------------------------ This is o/p
echo $final_date;
?>

Thanks bro, but can you show me a sample as I still getting error can't insert into database. This is my query:

 

I have a table names perjalanan. With field Date_of_Depart (Date) & Time_of_Depart (Time).

Inserting data command:

insert into perjalanan values (14may2007, 12:00);

 

I think mostly is the way I insert data wrong right?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.