note 85942 modified in function.strtotime by danbrown

From: Date: Thu, 25 Sep 2008 17:57:30 +0000
Subject: note 85942 modified in function.strtotime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-144708@lists.php.net to get a copy of this message
Some simple function to find all periods in dates list <?php function extract_periods($dates,$delimiter="="){ sort($dates); $period=Array(); $date_from=$date_to=""; $k=0; foreach ($dates as $key=>$value) { if (empty($date_from)) $date_from=$value; else { $k++; $tmp_date=strtotime("+ ".$k." day",strtotime($date_from)); if (date("Y-m-d", $tmp_date)!=$value) { $tmp_date=strtotime("+ ".($k-1)." day",strtotime($date_from)) $date_to=date("Y-m-d",$tmp_date); if ($date_from!=$date_to) $period[]=$date_from.$delimiter.$date_to; else $period[]=$date_from; $date_from=$value; $k=0; } } } return $period; } Use: $dates = Array( "2008-09-30", "2008-09-28", "2008-09-26", "2008-09-27", "2008-09-25"); $periods = extract_periods($dates); foreach ($periods as $value) echo $value."<br />"; ?> Output: 2008-09-25=2008-09-28 2008-09-30 --was-- Some simple function to find all periods in dates list function extract_periods($dates,$delimiter="="){ sort($dates); $period=Array(); $date_from=$date_to=""; $k=0; foreach ($dates as $key=>$value) { if (empty($date_from)) $date_from=$value; else { $k++; $tmp_date=strtotime("+ ".$k." day",strtotime($date_from)); if (date("Y-m-d", $tmp_date)!=$value) { $tmp_date=strtotime("+ ".($k-1)." day",strtotime($date_from)) $date_to=date("Y-m-d",$tmp_date); if ($date_from!=$date_to) $period[]=$date_from.$delimiter.$date_to; else $period[]=$date_from; $date_from=$value; $k=0; } } } return $period; } Use: $dates = Array( "2008-09-30", "2008-09-28", "2008-09-26", "2008-09-27", "2008-09-25"); $periods = extract_periods($dates); foreach ($periods as $value) echo $value."<br />"; Output: 2008-09-25=2008-09-28 2008-09-30 http://php.net/manual/en/function.strtotime.php

« previous php.notes (#144708) next »