2015-08-28 93 views
2

我有两个日期对象从日期输入和时间输入发送。PHP:将两个日期对象合并为一个

我想生成一个日期时间对象,其中输入日期为yyyy-mm-dd,输入时间为hh-mm。

数据从数据输入

[search_from_date] => 2015-08-04T23:00:00.000Z 

从数据输入时间派人送来

[search_from_time] => 1970-01-01T02:02:00.000Z 

我需要这两个日期合并到:

2015-08-04 02:02 

我一直在玩弄爆炸的字符串等无济于事,

任何帮助将appriciated

干杯

回答

0

这样的事情应该工作。

$finalDateD创造了刚刚年,月,日的日期字符串 和 $finalDateT创建只是一个时间日期字符串,分 这是然后串联到变量$finalDate;

$finalDateD = date('Y-m-d', strtotime($search_from_date)); 
$finalDateT = date('H:i', strtotime($search_from_time)); 
$finalDate = $finalDateD.' '.$finalDateT; 
3

您可以从您的字符串创建两个DateTime对象,然后使用第二个设置第一个的时间。

$arr = ['search_from_date' => '2015-08-04T23:00:00.000Z', 'search_from_time' => '1970-01-01T02:02:00.000Z']; 
$date = new DateTime($arr['search_from_date']); 
$time = new DateTime($arr['search_from_time']); 
$date->setTime($time->format('H'), $time->format('i'), $time->format('s')); 
echo $date->format('r'); 

下面是一个示例的eval.in - https://eval.in/424209

+0

啊真棒,是的,这看起来是正确的。我很快就会接受测试。谢谢! –

0

可以拆分的 “T” 这样的:

$search_from_date = "2015-08-04T23:00:00.000Z"; 
$search_from_time = "1970-01-01T02:02:00.000Z"; 

$tab_date = explode("T", $search_from_date); 
$tab_time = explode("T", $search_from_time); 

$date_time = new DateTime("$tab_date[0]T$tab_time[1]"); 

var_dump($date_time); 
0

试试这个,

$search_from_date = '2015-08-04T23:00:00.000Z'; 
$search_from_time = '1970-01-01T02:02:00.000Z'; 

$data = explode('T',$search_from_date); 
$data1 = explode('T',$search_from_time); 
$data1 = explode('.',$data1[1]); 

echo $data[0].' '.$data1[0]; 
0
$time=new DateTime('1970-01-01T02:02:00.000Z'); 
$date=new DateTime('2015-08-04T23:00:00.000Z'); 
$newDateTime= new DateTime(); 
$newDateTime->setTime($time->format('H'),$time->format('i'),$time->format('s')); 
$newDateTime->setDate($date->format('Y'),$date->format('m'),$date->format('d')); 
echo $newDateTime->format('Y/m/d H:i')); 
exit;