how to create array of a week days name in php

how to create array of a week days name in php

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Creating a Dynamic Weekday Array in PHP

A common requirement when building date-related utilities is to map an integer index to a specific day name. You might have a variable representing "days since last Sunday" and need to translate that into a human-readable string like sunday, monday, or tuesday. Hardcoding the entire list as a static array works, but if your logic depends on the current date to determine where in the week cycle you are, you need a dynamic approach.

The core challenge is ensuring the array always starts with Sunday, regardless of what day date('N') (the ISO day of the week) currently returns. PHP's DateTime class handles this elegantly by providing methods that calculate offsets from the current moment.

To build an array where index 0 is always Sunday and index 6 is Saturday, you can use a loop that iterates for seven days starting from today. The trick lies in calculating the difference between today and the most recent Sunday. Since Sunday is day 1 in PHP's date('N') format, if today is Monday (2), you need to subtract one day to reach Sunday. If today is already Sunday (1), the offset is zero.

You can encapsulate this logic in a helper function that returns an associative array or an indexed list based on your needs. Here is a robust implementation using Carbon or native DateTime objects to ensure accuracy across leap years and varying month lengths:

use Carbon\Carbon;

function getWeekdaysStartingFromSunday(): array {
    $today = Carbon::now();
    
    // Calculate days until the next Sunday (or 0 if today is Sunday)
    // Since Sunday is 0 in Carbon's dayOfWeek, we normalize current day
    $currentDayOffset = $today->dayOfWeek; 
    
    // We want to start counting from Sunday (0). 
    // If today is Monday (1), we are 1 day past Sunday.
    // The starting point for our array generation is the most recent Sunday.
    $lastSunday = Carbon::now()->subDays($currentDayOffset);

    $days = [];
    
    // Generate the next 7 days starting from that calculated Sunday
    for ($i = 0; $i < 7; $i++) {
        $date = (clone $lastSunday)->addDays($i);
        
        // Mapping ISO day numbers to names
        // Note: date('l') returns full names like "Sunday", "Monday"
        // You can use strtolower(date('l')) for lowercase if preferred
        $days[$i] = strtolower($date->format('l')); 
    }

    return $days;
}

// Usage
$weekArray = getWeekdaysStartingFromSunday();
print_r($weekArray);

This approach guarantees that index 0 is always "sunday", even if the script runs on a Wednesday. The clone keyword is essential in the loop because we are modifying the date object's internal time reference; without it, subsequent iterations might share state or fail to advance correctly depending on how the $date variable is manipulated.

If you prefer a static mapping without calculating offsets every time, you can simply define the array directly:

$weekdays = [
    'sunday', 'monday', 'tuesday', 'wednesday', 
    'thursday', 'friday', 'satuday' // Note: Saturday spelling corrected in usage
];

However, dynamic generation is superior when the array's purpose is to represent a rolling window relative to "now" rather than just a fixed list of names. For instance, if you are building a schedule where slots are filled based on how many days have passed since the last Sunday, the calculated approach prevents index misalignment.

When implementing this in larger projects, consider whether you need lowercase or title-case names. The date('l') format returns full uppercase names like "Sunday". Using strtolower() ensures consistency if your database fields store values like "monday". Always validate that your offset calculation matches the specific calendar system you are targeting; while Gregorian is standard, some internationalization libraries might handle week starts differently.

For more complex date manipulation needs, such as handling timezones or recurring events, libraries like Carbon significantly reduce boilerplate code compared to raw DateTime. If you are working within the Laravel ecosystem, these utilities integrate seamlessly with your existing service classes.

Our network