使用implode和explode更新时间格式

时间:2015-07-30 20:53:02

标签: php date explode strtotime implode

我有以下字符串

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]))

有没有办法简单地做到这一点而不重复它们?

4 个答案:

答案 0 :(得分:2)

你可以正确使用它。

echo preg_replace('~:\d{2}(,|$)~', '$1', '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

答案 1 :(得分:1)

$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

答案 2 :(得分:1)

$result = array();
foreach(explode(',', $req->slots) as $time) $result[] = date('H:i',strtotime($time));
$result = implode(',', $result);

答案 3 :(得分:1)

嗯......它不是在迭代:

APPLICATION_ROOT

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