15

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");
Sam
  • 53
  • 7
xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • 1
    If the array is always in the format `$weekarray = ("sunday","monday",......,"saturday");` then what does the current date have to do with this? You need to clarify yourself a bit here. – Tatu Ulmanen Mar 29 '10 at 09:04
  • i think he meant that element 0 will be the current day.. otherwise it makes no sense to create it dynamically – reko_t Mar 29 '10 at 09:05
  • No!...i just want to create a dynamic array of 7 days which started from sunday...current date matters to point out that which day is today using css. – xkeshav Mar 29 '10 at 09:08
  • He states that he always wants to start it at sunday, so it isn't quite clear what he wants. – wimvds Mar 29 '10 at 09:09
  • I think we can infer that he wants an array starting sunday of the current week. – khany Jun 26 '14 at 09:14

11 Answers11

56

This might work..

$timestamp = strtotime('next Sunday');
$days = array();
for ($i = 0; $i < 7; $i++) {
    $days[] = strftime('%A', $timestamp);
    $timestamp = strtotime('+1 day', $timestamp);
}
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
Thiago Belem
  • 7,732
  • 5
  • 43
  • 64
  • I think this answer will change the names of the days of the week depending on the php locale settings – Timo Huovinen Jun 03 '13 at 19:11
  • http://stackoverflow.com/questions/23603207/particular-week-days-with-repeat-occurences-php any idea for this question – Steve Bals May 12 '14 at 07:42
  • Using strftime is best because its will provide locale-specific day names. Not sure if "sunday" will make it through strtotime in another language. To simplify (knowing that the Epoch began on Thursday, and there are 86400 seconds in a day: $days = array_map(function ($x) {return strftime('%A', 86400 * ($x + 3));}, range(0,6)); – wordragon Aug 16 '20 at 15:04
52

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.

dockeryZ
  • 3,981
  • 1
  • 20
  • 28
Yacoby
  • 54,544
  • 15
  • 116
  • 120
  • 3
    i thought that it will be intresting to create via php – xkeshav Mar 29 '10 at 09:13
  • 10
    @I Like PHP It's nonsense to write code that always does the same thing and produces the same outcome. Unless it's easier to express that result in code than to write it directly, but that isn't the case here. – deceze Mar 29 '10 at 09:16
  • @deceze I agree with u sir, buti just want to learn something extra, shoud it bother u all great minds.....may i delet this question??? y all scream on me when i ask for something different – xkeshav Mar 29 '10 at 09:22
  • 1
    @I Like PHP It's okay to want to know how to do something to learn something new, no problem there. I think what's getting to us is that you should never use this technique in the real world (see above), and we want to make sure you understand this. If you'd have phrased the question as such ("I know this is stupid, but how would I...?"), I think nobody would have complained. No worries, keep on asking. Just never use this code. Ever. ;) – deceze Mar 29 '10 at 09:29
  • i m bad in english sir, pardon for that.... i just ask the question here bcoz i like to post here, bcoz this is really very good site for a life learner person – xkeshav Mar 29 '10 at 09:32
  • I know this is old, and this probably isn't very important, but would it not be better practice to avoid having a trailing comma in your array assignment there (in this case, after 'Saturday')...? I realize it wouldn't really hurt anything in an array, but I just think it's not a good habit to get into. – vertigoelectric Oct 27 '12 at 19:59
  • 2
    I consider keeping the trailing comma the better practice, because of its benefits for version control. If the last line is somehow special, then adding a new line requires changing that line too, even if the data on that line hasn't really changed. Increasing the number of changes adds to the complexity of most version control tasks... – Brilliand Dec 05 '12 at 19:14
  • This is usefull for local projects. But If you need localization, this is useless unless there is a language extension on php. – littlealien Aug 22 '14 at 09:19
  • does it cover the locale based name for the days? i mean in germany its sontag not sunday. or there is another way to do that? – R T Nov 19 '14 at 07:55
  • I think technically this is faster. Probably by like 1ms... no iteration and however long 2 `strtotime` and 1 `strftime` functions take. I'm not trying to be a smart ass, my point is that an enormous enterprise piece of software might appreciate a few millisecond cut. – Taylor Evanson May 20 '16 at 19:18
14

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

Rumen Tabakov
  • 331
  • 2
  • 6
3

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"
}
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • so i have to put date ...always ?? – xkeshav Mar 29 '10 at 09:14
  • 1
    You have an array of week days. The current date doesn't change the array in anyway. Why does it matter? – reko_t Mar 29 '10 at 09:16
  • Sir,i just want to do something different...everybody write the array and do the stufff... i want to do it via php coding...thats y. – xkeshav Mar 29 '10 at 09:19
  • 3
    @I Like PHP I see, self-made job security. I pity the developer who has to maintain your code eventually. ;P – deceze Mar 29 '10 at 09:21
3

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)
);
1
$now = time();
$days = array();
for ($i = 0; $i < 7; $i++) {
    $days[] = strftime('%A', $now);
    $now += 60*60*24;
}
reko_t
  • 55,302
  • 10
  • 87
  • 77
1
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];

}
FDisk
  • 8,493
  • 2
  • 47
  • 52
edeoleo
  • 11
  • 1
0
$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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • any idea for this question http://stackoverflow.com/questions/23603207/particular-week-days-with-repeat-occurences-php – Steve Bals May 12 '14 at 07:41
0

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');
Nicolò Martini
  • 5,182
  • 4
  • 32
  • 38
0

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
}

?>
ShapCyber
  • 3,382
  • 2
  • 21
  • 27
-1
$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"));
}
Tomasz Brzezina
  • 1,452
  • 5
  • 21
  • 44