2013-07-11 80 views
0

我知道如何使用PHP来计算日期差异;如何查找日期差异排除在数组中的特定日期 - PHP

$newdate = "01-03-2013"; 
$olddate = "01-06-2013"; 
$date_diff = abs(strtotime($olddate)-strtotime($newdate))/86400; 
echo $date_diff; 

但假设,如果我有一些数组中的日期,如;

$datesarray = array(10-05-2013, 20-05-2013, 12-08-2013); 

等,保持一些特定日期,是能够计算不包括与星期日沿阵列的日期日期区别,如果它们位于在开始和结束日期之间?

+0

这是一个重复的问题http://stackoverflow.com/questions/10595524/date-range-array-excluding-the-sunday-the-holiday -in-php?rq = 1 – DevZer0

+0

如果您使用Ymd格式(如MySQL),那么您可以使用'max($ datesarray)'和'min($ datesarray)'作为结尾和开始。 AND strtotime()更适用于此,因为d-m-y和m-d-y是使用格式的,但是不给出相同的时间结果。 __WARNING__看着你的数据'20-05-2013'你正在使用UK格式,所以strtotime()不适合你! – Waygood

+0

你很幸运,strtotime不会改变这些日期值。那些是1月3日和1月6日?或3月1日和6月1日?永远不要依靠时间去做“正确的事”。 –

回答

0

只是循环通过$datesarray并检查每一个是否在$olddate$newdate之间。如果是这样,增加一个$counter变量(显然从0开始)。 Then $date_diff - $counter会给你预期的结果。

0

我会使用DateTime类中一个自定义功能是这样的:

function dates_between(DateTime $start, DateTime $end, $format = 'm-d-Y') { 
    $date = $start; 
    $dates = array(); 
    $oneDay = new DateInterval('P1D'); 
    // push all dates between start and end to the result 
    while(($date = $date->add($oneDay)) < $end) { 
     $dates []= $date->format($format); 
    } 
    return $dates; 
} 

实例:

$now = new DateTime(); 
$nextWeek = new DateTime('+1 week'); 
var_dump(dates_between($now, $nextWeek)); 

输出:

array(6) { 
    [0] => 
    string(10) "07-12-2013" 
    [1] => 
    string(10) "07-13-2013" 
    [2] => 
    string(10) "07-14-2013" 
    [3] => 
    string(10) "07-15-2013" 
    [4] => 
    string(10) "07-16-2013" 
    [5] => 
    string(10) "07-17-2013" 
} 
+0

不工作的兄弟.. :(你可以在codepad.org上演示一个演示程序吗?这里是你的代码http:// codepad。组织/ XL9gkJ9d –

0

以下脚本创建和阵列来自您英国日期数组的时间戳,然后计算出最后的日期和时间戳记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]); 
} 
?>