Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从长串数字中格式化一系列日期?_Php_Date_Format_Word Wrap - Fatal编程技术网

Php 如何从长串数字中格式化一系列日期?

Php 如何从长串数字中格式化一系列日期?,php,date,format,word-wrap,Php,Date,Format,Word Wrap,我有一个代表多个日期的数字串。假设是:11262014112920141225214。该数字代表2014年11月26日至2014年11月29日至2014年12月25日 我想格式化它,使它看起来像[11,26,2014]、[11,29,2014]、[12,25,2014] 通过wordwrap,我几乎可以按我想要的方式获得它: $date = wordwrap( $date , 8, '], [' , true ); $date = "[" . $date . "]"; echo $date;

我有一个代表多个日期的数字串。假设是:11262014112920141225214。该数字代表2014年11月26日至2014年11月29日至2014年12月25日

我想格式化它,使它看起来像[11,26,2014]、[11,29,2014]、[12,25,2014]

通过wordwrap,我几乎可以按我想要的方式获得它:

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

echo $date;
返回[11262014]、[11292014]、[12252014]


我还可以用什么来获得我想要的格式呢?

考虑到你的天数和月份总是用两位数表示,这里有一个解决方案。例如,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;

?>

希望有助于
:-)

了解以下功能:

回波内爆(“,”,数组映射(函数($val){ $val=substr_replace($val',,',4,0); $val=substr_replace($val',,',2,0); 返回“[”.$val.]”; },str_split($input_string,8)) );

使用substr()轻松[alternative preg_splat or plain regular expressions](我希望少于10天和月份的天数和月份的前导值为0