2011-03-08 42 views
0

我从数据库中获取的日期在这个格式腓添加不时

hh:mm:ss 

我想要将它添加到当前的时间和放回的其他表的格式如下:

yyyy:mm:dd hh:mm:ss 
+1

你的意思是“添加日期和时间”?哪一天?目前? – phihag 2011-03-08 14:20:12

+0

这么宽的问题.............. – 2011-03-08 14:21:16

+0

这个线程可能可以帮到你:http://stackoverflow.com/questions/2303724/formating-an-sql-timestamp-with-php – Enthusiast 2011-03-08 14:21:35

回答

1

假设你从数据库中获取的时间在变量$ DBTIME我会做这样的事情:

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

未必是最干净的方式,但它是一种选择:)

+0

谢谢,这很好。 – 2011-03-08 14:41:49

2
$a = strtotime($timetoadd); 
$b = date('U') + $a; 
$c = date('Y-m-d H:i:s',$b); 

编辑:代替试试这个:

$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); 
+0

我曾试过,但我得到了这个:1916-04-06 17:16:06,这是一个过去的日期... – 2011-03-08 14:32:12

+0

检查我的编辑。给它一个镜头,让我知道它是否工作 – Munim 2011-03-08 16:09:33

+0

是的,工作。谢谢! – 2011-03-08 23:21:48

0

从你的问题,我了解您希望将数据库中的时间值添加到当前日期时间值中。这是这样做的代码: -

// 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); 

希望这会有所帮助。谢谢。