2014-11-24 58 views
1

我有一串代表多個日期的數字。假設它是:112620141129201412252014.該數字表示2014年11月26日 - 2014年11月29日 - 2014年12月25日。如何從長串數字格式化一系列日期?

我想對其進行格式化,使其看起來像[11,26,2014],[ 2014年11月29日],[12,25,2014]。

我能得到它幾乎是辦法,我想它的換行:

$date = wordwrap($date , 8, '], [' , true); 
$date = "[" . $date . "]"; 

echo $date; 

將返回:[11262014],[11292014],[12252014]

我還可以用它來得到它以我想要的格式?

+0

易與SUBSTR()[替代preg_splat或純的正則表達式] (我希望幾天和幾個月不到10天有一次領先0 – 2014-11-24 01:24:52

回答

0

這是一個解決方案,考慮到你的日子和月份總是以兩位數字表示。例如,2014年11月3日應該是11032014而不是1132014

<?php 

$list = "112620141129201412252014"; 
$newlist = ""; 

while (!empty($list)) 
{ 
    $newlist = $newlist . "[" . substr($list, 0, 2) . "," . substr($list, 2, 2) . "," . substr($list, 4, 4) . "]"; 
    $list = substr($list, 8); // remove from $list the date we just added to $newlist 
    if (!empty($list)) 
    { 
     $newlist = $newlist . ","; 
    } 
} 

echo $newlist; 

?> 

希望有所幫助:-)

0

str_split()substr_replace()功能請看:

echo implode(', ', array_map(function($val) { 
     $val = substr_replace($val, ',', 4, 0); 
     $val = substr_replace($val, ',', 2, 0); 
     return '[' . $val . ']'; 
    }, str_split($input_string, 8)) 
); 

demo