2

I want to get list of days in week by giving a Date. Like If gave "06/11/2013" I want get the list as PHP Array, any help will be appreciated

vascowhite
  • 18,120
  • 9
  • 61
  • 77
Shahzab Asif
  • 481
  • 5
  • 13
  • 1
    I think that SO has already covered *almost* everything about dates in php, have you explored the questions ? – HamZa Jun 11 '13 at 08:22
  • You require the day for the date specified or all the days in the week in which the specified date exists ? – Akhilesh Sharma Jun 11 '13 at 08:23

3 Answers3

3

I would suggest you do a quick google search or even search on StackOverflow before asking a questions. As HamZa said in the comments - SO has already covered almost everything about dates.

You can get your answer by using:

function week_from_monday($date) {
    // Assuming $date is in format DD-MM-YYYY
    list($day, $month, $year) = explode("-", $date);

    // Get the weekday of the given date
    $wkday = date('l',mktime('0','0','0', $month, $day, $year));

    switch($wkday) {
        case 'Monday': $numDaysToMon = 0; break;
        case 'Tuesday': $numDaysToMon = 1; break;
        case 'Wednesday': $numDaysToMon = 2; break;
        case 'Thursday': $numDaysToMon = 3; break;
        case 'Friday': $numDaysToMon = 4; break;
        case 'Saturday': $numDaysToMon = 5; break;
        case 'Sunday': $numDaysToMon = 6; break;   
    }

    // Timestamp of the monday for that week
    $monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year);

    $seconds_in_a_day = 86400;

    // Get date for 7 days from Monday (inclusive)
    for($i=0; $i<7; $i++)
    {
        $dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i));
    }

    return $dates;
}

Output from week_from_monday('07-10-2008') gives:

Array
(
    [0] => 2008-10-06
    [1] => 2008-10-07
    [2] => 2008-10-08
    [3] => 2008-10-09
    [4] => 2008-10-10
    [5] => 2008-10-11
    [6] => 2008-10-12
)

Taken from: Calculating days of week given a week number

Community
  • 1
  • 1
ajtrichards
  • 29,723
  • 13
  • 94
  • 101
1

The DateTime classes make this much simpler to do:-

/**
 * Returns an array of date strings for the
 * week of the given date, starting on Monday
 *
 * @param String $date The date in question
 * @return array An array of date strings
 */
function daysInWeek($date)
{
    $result = array();
    $datetime = DateTime::createFromFormat('m/d/Y', $date);
    $interval = new DateInterval('P1D');
    if($datetime->format('D') !== 'Mon')$datetime->modify('last monday');
    $week = new DatePeriod($datetime, $interval, 6);
    foreach($week as $day){
        $result[] = $day->format('m/d/y');
    }
    return $result;
}

var_dump(daysInWeek("06/11/2013"));

Returns:-

array (size=7)
  0 => string '06/10/13' (length=8)
  1 => string '06/11/13' (length=8)
  2 => string '06/12/13' (length=8)
  3 => string '06/13/13' (length=8)
  4 => string '06/14/13' (length=8)
  5 => string '06/15/13' (length=8)
  6 => string '06/16/13' (length=8)

This has the added advantage of taking care of Daylight savings changes etc..

vascowhite
  • 18,120
  • 9
  • 61
  • 77
-1

Do you want to fetch the full name of the day corresponding to a given date? If so you could do something like this:

<?php
    echo date('l', strtotime('2013/11/06'));
?>
Kevin Op den Kamp
  • 565
  • 1
  • 8
  • 22
  • I am sorry! you did not get it , want list of days in week for given date. – Shahzab Asif Jun 11 '13 at 08:30
  • As you can see your question raises other questions in terms of what you're actually trying to achieve. Either provide as much details as you can or save yourself the trouble of posting on stackoverflow entirely, and in turn save people who are trying to help you from a decrease in reputation. – Kevin Op den Kamp Sep 15 '13 at 19:22