使用 implode 和 explode 更新时间格式

Update time format using implode and explode

我有如下字符串

12:00:00,11:30:00,10:30:00,10:00:00,09:30:00

我需要将其转换为

12:00,11:30,10:30,10:00,09:30

可以使用

转换单个值
date('H:i',strtotime(explode(',',$req->slots)[0]))

有什么方法可以简单地完成它而无需遍历它们吗?

$str = '12:00:00,11:30:00,10:30:00,10:00:00,09:30:00';
$ar = explode(',', $str);

foreach($ar as &$item) 
   $item = substr($item, 0,-3);

echo $str = implode(',', $ar); // 12:00,11:30,10:30,10:00,09:30
$result = array();
foreach(explode(',', $req->slots) as $time) $result[] = date('H:i',strtotime($time));
$result = implode(',', $result);

你可以正则表达式。

echo preg_replace('~:\d{2}(,|$)~', '', '12:00:00,11:30:00,10:30:00,10:00:00,09:30:00');

输出:

12:00,11:30,10:30,10:00,09:30

正则表达式演示:https://regex101.com/r/vW0kN4/2
PHP 演示:http://sandbox.onlinephpfunctions.com/code/91d3f2ceb8c7f763e51c32841c4ee201070ab514

嗯...有点不迭代:

 var str = "12:00:00,11:30:00,10:30:00,10:00:00,09:30:00";
 var new_str = str.split(",").map(function(x){return x.replace(/^(.*):.*$/, "")}).join(",");

JSFiddle: http://jsfiddle.net/trex005/9kdcaowf/