Home >php教程 >php手册 >一个求时间段的方法(MM-DD)格式

一个求时间段的方法(MM-DD)格式

WBOY
WBOYOriginal
2016-06-21 09:06:591011browse


function m_d($v,$d) {
  $d = array(31,31,28,31,30,31,30,31,31,30,31,30,31);
  $t = split("\ ",$v);
  if($t[1] == $d[$t[0]] && $d>0) {
    $t[0]++;
    $t[1] = 1;
  }else if($t[1] == 1 && $d    $t[0]--;
    $t[1] = $d[$t[0]];
    if($t[0] == 0) $t[0] = 12;
  }else
    $t[1] .= $d;
  return sprintf("%02d %02d",$t[0],$t[1]);
}

function foo($ar) {
  static $p = 0;
  $retval = false;
  if($p == 0) {
    $p = $ar['end_dt'];
    return $retval;
  }
  if($p     $retval = array('start_dt' => m_d($p,1), 'end_dt' => m_d($ar['start_dt'],-1) );
  $p = $ar['end_dt'];
  return $retval; 
}
$a=array();
$a[0]['start_dt']="9 25";
$a[0]['end_dt']="10 05";
$a[1]['start_dt']="10 15";
$a[1]['end_dt']="10 30";
$a[2]['end_dt']="10 20";
$a[2]['end_dt']="11 20";
foreach($a as $v) {
  if($r = foo($v))
    $ar[] = $r;
}
print_r($ar);

?>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn