我有一个字符串,它将dateTime作为一个GROUP从数据库中获取,所以我得到类似这样的东西
2017-10-20 05:00:00,2017-10-20 09:00:00,2017-10-20 07:00:00,2017-10-20 13:30:00,2017-10-20 16:00:00,2017-10-20 13:00:00,2017-10-20 06:00:00,2017-10-20 09:30:00,2017-10-20 10:30:00,2017-10-20 15:30:00,2017-10-20 17:00:00
请注意,所有日期都相同,即2017-10-20。因为我不需要担心不同的日期。我想要的只是简单地从这个字符串中提取时间,并像这样很好地打印它
05:00, 09:00, 07:00, 13:30, 16:00 //and so on
我不想要秒,这是不必要的。
我尝试使用PHP的strtotime()函数,但它失败并改为00:00:00。
有什么想法吗?我使用Laravel作为模板引擎。
答案 0 :(得分:6)
在您的SQL语句中执行此操作。
SELECT TIME_FORMAT(`dateColumn`, '%H:%i') FROM ...
答案 1 :(得分:1)
一步一步:
$dateTimes = '2017-10-20 05:00:00,2017-10-20 09:00:00,2017-10-20 07:00:00,2017-10-20 13:30:00,2017-10-20 16:00:00,2017-10-20 13:00:00,2017-10-20 06:00:00,2017-10-20 09:30:00,2017-10-20 10:30:00,2017-10-20 15:30:00,2017-10-20 17:00:00';
$dateTimesArray = explode(',', $dateTimes);
$timesArray = [];
foreach($dateTimesArray as $dateTime) {
$timesArray[] = date('H:i', strtotime($dateTime));
}
$times = join(',', $timesArray);
答案 2 :(得分:0)
<?php
$tGroup = "2017-10-20 05:00:00,2017-10-20 09:00:00,2017-10-20 07:00:00,2017-10-20 13:30:00,2017-10-20 16:00:00,2017-10-20 13:00:00,2017-10-20 06:00:00,2017-10-20 09:30:00,2017-10-20 10:30:00,2017-10-20 15:30:00,2017-10-20 17:00:00";
$retVal = "";
$dates = explode(",", $tGroup);
foreach ($dates as $date) {
$time = date('H:i', strtotime($date));
// use this if you want to print AM/PM format
//$time = date('h:i A', strtotime($date));
$retVal .= $time . ", ";
}
echo trim($retVal," ,");