My Google+ Profile

Monday 3 December 2012

Get all sunday date of current Year Using PHP

Following function return array of list of Sunday's Date of the Current Year in PHP.

<?php

function getDateForSpecificDayBetweenDates($startDate, $endDate, $weekdayNumber)
{
    $startDate = strtotime($startDate);
    $endDate = strtotime($endDate);

    $dateArr = array();

    do
    {
        if(date("w", $startDate) != $weekdayNumber)
        {
            $startDate += (24 * 3600); // add 1 day
        }
    } while(date("w", $startDate) != $weekdayNumber);


    while($startDate <= $endDate)
    {
        $dateArr[] = date('Y-m-d', $startDate);
        $startDate += (7 * 24 * 3600); // add 7 days
    }

    return($dateArr);
}

$year   = date("Y");

$dateArr = getDateForSpecificDayBetweenDates($year.'-01-01', $year.'-12-31', 0);

print "<pre>";
print_r($dateArr);

?>

And if you want to get any other day of  week then just change value of weekday. I have pass 0 value into the function to get all Sunday of the current year.

Note : Pass weekdays value between 0  to 6. 

1 comment: