Tanmay
Tanmay

Reputation: 3189

Saving time as timestamp in database with PHP

From the front-end I am taking a time string by three separate input fields: hour, minute and mode (for am/pm). Then in the back-end I am concatenating all these three inputs to make up something like this: 12:05pm

The DB column in which I want to store this time is in timestamp format. If I insert this string, mysql force converts it to 0000-00-00 00:00:00. How can I insert the time?

Upvotes: 0

Views: 1199

Answers (1)

shubhangee
shubhangee

Reputation: 539

Get the date and and concatenate the time with date and conver it into strtotime like below

$timestamp = '02:01pm';
$currentDateTime = date('Y-m-d');
$newDate= date('Y-m-d H:i:s',strtotime($currentDateTime." ".$timestamp));
echo $newDate;

Upvotes: 2

Related Questions