Php add time to time

2019-09-10 20:44发布

I get a date from the database in this format

hh:mm:ss

And I want to add this to the current time and put back to an other table with the following format:

yyyy:mm:dd hh:mm:ss

标签: php time
3条回答
走好不送
2楼-- · 2019-09-10 21:06
$a = strtotime($timetoadd);
$b = date('U') + $a;
$c = date('Y-m-d H:i:s',$b);

Edit: Try this instead:

$arr = explode(':',$timetoadd);
$b = mktime(date('h')+$arr[0],date('i')+$arr[1],date('s')+$arr[2],date('m'),date('d'),date('y'));
$c = date('Y-m-d H:i:s',$b);
查看更多
劫难
3楼-- · 2019-09-10 21:18

Assuming you get the time from the database in a variable $dbTime I'd do something like this:

$timeArray = explode (":", $dbTime );
$newTime = time() + ($timeArray[0]*60*60) + ($timeArray[1]*60) + $timeArray[2];
$finalTime = date('Y-m-d H:i:s',$newTime);

May not be the cleanest way but it is an option :)

查看更多
淡お忘
4楼-- · 2019-09-10 21:24

From your question, I understand that you want your time value from database to be added in the current date time value. Here is the code to do so:-

// hh:mm:ss value you get from your database
$yourDateTime = "SOME DATA";

// current date time
$currentDateTime = date("Y:m:d h:i:s", time());

// separating date and time
$currentDateTimeArray = explode('', $currentDateTime);

// adding your time value to current time value
$currentDateTimeArray[1] = $yourDateTime;

$newDateTime = implode('', $currentDateTimeArray);

Hope this helps. Thanks.

查看更多
登录 后发表回答