用php处理时间文本

从数据库中直接导出的文本(数据的日期范围及服务次数):
2 2003-07-19 2003-08-18
10 2004-03-22 2004-03-22
9 2005-11-05 2005-11-05
4 2006-10-01 2008-10-31
18 2007-01-01 2009-07-15

想把其中的服务次数累加到每一天上去,本来想用sql直接处理,结果没有想到什么好的方法,只好请小李写了一个脚本来处理:

<?php
$file_handle = fopen("./water-time-service.txt", "r");
$arr = array();
$index = 0;
while (!feof($file_handle)) {
	$index ++;
   $line = fgets($file_handle);
   $times = (int)mb_substr($line,0,2,"UTF-8");
   $date_start = trim(mb_substr($line,2,11,"UTF-8"));
   $date_end = trim(mb_substr($line,13,11,"UTF-8"));
   $end_day = strtotime($date_end);
   $days = round((strtotime($date_end) - strtotime($date_start)) / (3600*24)) + 1;
   $_thisline = array();
   for(;;)
   {
	   if($days < 1)
	   {
		   break;
	   }
	   $_thisline[] = date("Y-m-d",$end_day - ($days - 1) * (3600 * 24));
	   $days --;
   }
   foreach($_thisline as $v)
   {
	   @$arr[$v] += $times;
   }
}
fclose($file_handle);
foreach($arr as $n=>$v) echo $n. " ". $v."\r\n";
?>

这样就能处理成这样的结果:
2003-07-19 2
2003-07-20 2
2003-07-21 2
2003-07-22 2
2003-07-23 2

然后就可以用gnuplot画图了。water-time-service


已发布

分类

, ,

来自

标签:

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注