PHP:将两个日期对象合并为一个
我从一个日期输入和一个时间输入发送了两个日期对象 我想从日期输入生成一个日期时间对象,该对象的yyyy-mm-dd和hh-mm分别来自于时间输入 从日期输入发送的数据PHP:将两个日期对象合并为一个,php,date,datetime,Php,Date,Datetime,我从一个日期输入和一个时间输入发送了两个日期对象 我想从日期输入生成一个日期时间对象,该对象的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 我一直在玩弄爆炸绳子之类的把戏,但都没用
[search_from_date] => 2015-08-04T23:00:00.000Z
从时间输入发送的数据
[search_from_time] => 1970-01-01T02:02:00.000Z
我需要将这两个日期合并为:
2015-08-04 02:02
我一直在玩弄爆炸绳子之类的把戏,但都没用
任何帮助都将受到感谢
干杯像这样的事情应该行得通
$finalDateD
仅为年、月和日创建日期字符串
和
$finalDateT
仅为小时、分钟创建一个日期字符串
然后将其连接到变量$finalDate
$finalDateD = date('Y-m-d', strtotime($search_from_date));
$finalDateT = date('H:i', strtotime($search_from_time));
$finalDate = $finalDateD.' '.$finalDateT;
您可以从字符串创建两个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示例-您可以像这样在“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);
试试这个
$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];
啊,太棒了,是的,这看起来是正确的。我很快就会接受测试。谢谢大家!!
$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;