Flipkart

Friday, February 18, 2011

Get number of weeks between 2 dates

function weeks_between($datefrom, $dateto)
{
    $datefrom_arr = explode("-", $datefrom);
    $datefrom = mktime(0,0,0,$datefrom_arr[1], $datefrom_arr[2], $datefrom_arr[0]);
                //echo date('Y-m-d',mktime(0,0,0,$datefrom_arr[1], $datefrom_arr[2]+7, $datefrom_arr[0]));
               
                $dateto_arr = explode("-",$dateto);
                $dateto = mktime(0,0,0,$dateto_arr[1], $dateto_arr[2], $dateto_arr[0]);
               
    $day_of_week = date("w", $datefrom);
    $fromweek_start = $datefrom - ($day_of_week * 86400) - ($datefrom % 86400);
    $diff_days = days_between($datefrom, $dateto);
    $diff_weeks = intval($diff_days / 7);
    $seconds_left = ($diff_days % 7) * 86400;

    if( ($datefrom - $fromweek_start) + $seconds_left > 604800 )
        $diff_weeks ++;

    return $diff_weeks;
}

function days_between($datefrom,$dateto){
    $fromday_start = mktime(0,0,0,date("m",$datefrom),date("d",$datefrom),date("Y",$datefrom));
    $diff = $dateto - $datefrom;
    $days = intval( $diff / 86400 ); // 86400  / day

    if( ($datefrom - $fromday_start) + ($diff % 86400) > 86400 )
        $days++;

    return  $days;
}

No comments:

Post a Comment