使用php将日期格式化为数组

时间:2017-12-24 14:23:31

标签: php arrays

我有这个数组;

Array
(
    [2017-04-01] => 0
    [2017-04-02] => 0
    [2017-04-03] => 0
    [2017-04-04] => 0
    [2017-04-05] => 0
)

如何格式化日期以获得类似的内容?

Array
(
    [01-04-2017] => 0
    [02-04-2017] => 0
    [03-04-2017] => 0
    [04-04-2017] => 0
    [05-04-2017] => 0
)

感谢。

1 个答案:

答案 0 :(得分:1)

一个简单的foreach循环然后转换为d-m-Y格式可以执行此操作

$result = [];
foreach ($datas as $key => $value) {
    $date = date('d-m-Y', strtotime($key));
    $result[$date] = $value;
}

print_r($result);