Date and time of the merger

$combinedDT = date('Ymd H:i:s', strtotime('$date $time')); 

Date Format 2013-10-14

time format 23:40:19

I get zeros when trying to save in datetime datatype

+7
date html php datetime time
source share
2 answers

You are currently executing strtotime('$date $time') . Variables enclosed in single quotation marks are not interpolated. If you use single quotes, PHP will treat it as a literal string, and strototime() will try to convert the string $date $time to a timestamp.

This will fail and it will explain why you are getting the wrong results.

Instead, you need to use double quotes:

 $combinedDT = date('Ymd H:i:s', strtotime("$date $time")); ^ ^ 
+22
source share

And for those who get started with DateTime objects:

 $date = new DateTime('2017-03-14'); $time = new DateTime('13:37:42'); // Solution 1, merge objects to new object: $merge = new DateTime($date->format('Ym-d') .' ' .$time->format('H:i:s')); echo $merge->format('Ymd H:i:s'); // Outputs '2017-03-14 13:37:42' // Solution 2, update date object with time object: $date->setTime($time->format('H'), $time->format('i'), $time->format('s')); echo $date->format('Ymd H:i:s'); // Outputs '2017-03-14 13:37:42' 
+14
source share

All Articles