xkeshav
xkeshav

Reputation: 54032

how to create array of a week days name in php

I know this is stupid, but how would I do this? I would like to create an array of seven days through PHP What I mean is all the seven weekdays. I don't want to write them like his:

sunday monday tuesday ...etc

and days will be starting from sunday which means if today is the 29th of march (monday) then it automatically grabs the current date and create an array of weekdays starting from Sunday.

array always be in this way

 $weakarray=("sunday","monday",......,"saturday");

Upvotes: 15

Views: 82237

Answers (11)

Yacoby
Yacoby

Reputation: 55445

If they always need to start with Sunday why do you want to create the array dynamically? What is wrong with doing this?

$days = [
    'Sunday',
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday'
];

Any other solution is going to make your code harder to understand and in this case doing it dynamically seems to be overkill.

Upvotes: 53

user2920996
user2920996

Reputation:

I would consider array map as more elegant way to achieve the same result.

$days = array_map(function ($day) {
    return strtolower(date_create('Sunday')->modify("+$day day")->format('l'));},
    range(0, 6)
);

Upvotes: 3

Rumen Tabakov
Rumen Tabakov

Reputation: 331

A little late answer, modification of the accepted as the best answer

<?php
    $days = array();
    for ($i = 0; $i < 7; $i++) {
        $days[$i] = jddayofweek($i,1);
    }
?>

Result:

array(7) { 
  [0]=> "Monday"
  [1]=> "Tuesday" 
  [2]=> "Wednesday" 
  [3]=> "Thursday" 
  [4]=> "Friday" 
  [5]=> "Saturday" 
  [6]=> "Sunday" 
}

See PHP's jddayofweek

Upvotes: 14

ShapCyber
ShapCyber

Reputation: 3662

Reference TuiTalk answer here is how you can use it.

Just choose how you want to use it, there are three optional usage.

<?php
$timestamp = strtotime('next Sunday');
$days = array();
for ($i = 0; $i < 7; $i++) {
    $days[] = strftime('%A', $timestamp);
    $timestamp = strtotime('+1 day', $timestamp);
    echo date("D",$timestamp)."<br>";
    //Mon<br>Tue<br>Wed<br>Thu<br>Fri<br>Sat<br>Sun<br>
    echo date("l",$timestamp)."<br>";
    //Monday<br>Tuesday<br>Wednesday<br>Thursday<br>Friday<br>Saturday<br>Sunday<br>
    echo date("d",$timestamp)."<br>";
    //02<br>03<br>04<br>05<br>06<br>07<br>08
}

?>

Upvotes: 0

Thiago Belem
Thiago Belem

Reputation: 7832

This might work..

$timestamp = strtotime('next Sunday');
$days = array();
for ($i = 0; $i < 7; $i++) {
    $days[] = strftime('%A', $timestamp);
    $timestamp = strtotime('+1 day', $timestamp);
}

Upvotes: 57

Tomasz Brzezina
Tomasz Brzezina

Reputation: 1534

$days = array();
for ($i = 0; $i < 7; $i++) {
  $days[strftime("%w",strtotime("+$i day"))] = strftime('%A', strtotime("+$i day"));
}

in 5.1.0+ PHP you can use %N, which sets sunday to 7 rather than 0

$days = array();
for ($i = 0; $i < 7; $i++) {
  $days[strftime("%N",strtotime("+$i day"))] = strftime('%A', strtotime("+$i day"));
}

Upvotes: -1

edeoleo
edeoleo

Reputation: 11

function dias_semana($days) {
    $days=explode(',',$days);
    $semana="";
    foreach ($days as $key=>$day){ 
        $semana.=dia_semana($day)."<br​>";
    }
    return $semana;

}
function dia_semana($dia) {
    $days = array(
        'Sunday',
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday'
    );
    return $days[$dia];

}

Upvotes: 1

Nicol&#242; Martini
Nicol&#242; Martini

Reputation: 5220

use the DateTime object

$date = new DateTime();
$weekdays = array();
for($i=0; $i<7; $i++){
    $weekdays[] = $date->format('l');
    $date->modify('+1 day');
}

If you want to start with Sunday, create DateTime with a sunday day (for example, yesterday):

$date = new DateTime('2010-03-28');

Upvotes: 0

deceze
deceze

Reputation: 522165

$days = array();
for ($x = 0; $x < 7; $x++) {
    $days[] = date('l', strtotime("+$x days", strtotime('2010-03-28')));
}

Seriously though, unless your question is being misunderstood, I completely second Yacoby's answer.

Upvotes: 0

codaddict
codaddict

Reputation: 455132

Try:

for($i=1;$i<8;$i++)
$weekdays [] = date("l",mktime(0,0,0,3,28,2009)+$i * (3600*24));
var_dump($weekdays);

Output:

C:\>php b.php
array(7) {
  [0]=>
  string(6) "Sunday"
  [1]=>
  string(6) "Monday"
  [2]=>
  string(7) "Tuesday"
  [3]=>
  string(9) "Wednesday"
  [4]=>
  string(8) "Thursday"
  [5]=>
  string(6) "Friday"
  [6]=>
  string(8) "Saturday"
}

Upvotes: 3

reko_t
reko_t

Reputation: 56430

$now = time();
$days = array();
for ($i = 0; $i < 7; $i++) {
    $days[] = strftime('%A', $now);
    $now += 60*60*24;
}

Upvotes: 1

Related Questions