以下脚本创建和阵列来自您英国日期数组的时间戳,然后计算出最后的日期和时间戳记max和分钟时间戳来计算天差。
如果时间戳记默认为0,则不会将其添加到时间戳记数组中,从而避免了对默认为时间戳记 即一个错误日期的巨大结果。当日期是无效的或预划时代1/1/1970
<?php
$datesarray = array('10-05-2013', '20-05-2013', '12-08-2013');
$date_diff=0; // default for 0 or 1 dates
if((is_array($datesarray)) && (sizeof($datesarray)>1))
{
$timestampsarray=array();
reset($datesarray);
while(list($key,$value)=each($datesarray))
{
$timestamp=timestamp_from_UK($value);
if($timestamp!=0) $timestampsarray[$key]=$timestamp;
}
$date_diff = abs(max($timestampsarray)-min($timestampsarray))/86400;
}
echo $date_diff;
function timestamp_from_UK($ukdatetime)
{
// where PHP is processing UK dates d-m-y correctly
$ukdatetime=str_replace('/', '-', $ukdatetime);
if(date("d", strtotime("1-2-1970"))==1) return strtotime($ukdatetime);
// Fallback script for when PHP is NOT processing UK dates
$success=false;
if(!$success) $success=preg_match("/([0-9]{1,2})[^0-9]([0-9]{1,2})[^0-9]([0-9]{2,4})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,2})/", $ukdatetime, $matches);
if(!$success) $success=preg_match("/([0-9]{1,2})[^0-9]([0-9]{1,2})[^0-9]([0-9]{2,4})[^0-9]([0-9]{1,2})[^0-9]([0-9]{1,2})/", $ukdatetime, $matches);
if(!$success) $success=preg_match("/([0-9]{1,2})[^0-9]([0-9]{1,2})[^0-9]([0-9]{2,4})/", $ukdatetime, $matches);
if(!$success) return 0;
// ensure all values are set - to avoid invalid offset
for($i=4;$i<=6;$i++)
{
if(!isset($matches[$i])) $matches[$i]=0;
}
// $matches[0] is the full matched string
return mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[1], $matches[3]);
}
?>
这是一个重复的问题http://stackoverflow.com/questions/10595524/date-range-array-excluding-the-sunday-the-holiday -in-php?rq = 1 – DevZer0
如果您使用Ymd格式(如MySQL),那么您可以使用'max($ datesarray)'和'min($ datesarray)'作为结尾和开始。 AND strtotime()更适用于此,因为d-m-y和m-d-y是使用格式的,但是不给出相同的时间结果。 __WARNING__看着你的数据'20-05-2013'你正在使用UK格式,所以strtotime()不适合你! – Waygood
你很幸运,strtotime不会改变这些日期值。那些是1月3日和1月6日?或3月1日和6月1日?永远不要依靠时间去做“正确的事”。 –