Thursday, September 6, 2012

Get Days of the Week

This function can be used to get the days of the week:

function get_week_days()
{
    $timestamp = strtotime('monday this week');
    $week_days = array();
    for ($i = 1; $i <= 7; $i++)
    {
        $day = array();
        $day['name'] = strftime('%A', $timestamp);
        $day['short_name'] = strftime('%a', $timestamp);
        $week_days[$i] = $day;
        $timestamp = strtotime('+1 day', $timestamp);
    }

    return $week_days;
}


Output of this function will be:

Array
(
    [1] => Array
        (
            [name] => Monday
            [short_name] => Mon
        )

    [2] => Array
        (
            [name] => Tuesday
            [short_name] => Tue
        )

    [3] => Array
        (
            [name] => Wednesday
            [short_name] => Wed
        )

    [4] => Array
        (
            [name] => Thursday
            [short_name] => Thu
        )

    [5] => Array
        (
            [name] => Friday
            [short_name] => Fri
        )

    [6] => Array
        (
            [name] => Saturday
            [short_name] => Sat
        )

    [7] => Array
        (
            [name] => Sunday
            [short_name] => Sun
        )

)
 
 
Hope this function will be useful to list the days of the week somewhere in your projects.
Happy coding..:)