tags:

views:

207

answers:

6

I know this is stupid, but how would I do this...? i want to create an array of 7 days through php means all 7 days name... i don't want to write ..sunday monday tuesday...etc... and days will be started from sunday .. means if today is 29 march( monday) then it automatically grabs the current date and create an array of days name starting from sunday. array always be in this way $weakarray=("sunday","monday",......,"saturday");

A: 
$now = time();
$days = array();
for ($i = 0; $i < 7; $i++) {
    $days[] = strftime('%A', $now);
    $now += 60*60*24;
}
reko_t
i said array should be alwayz started from sunday.....
diEcho
then why do you want to create the array dynamically..? it's just extra overhead.
reko_t
+11  A: 

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

$days = array(
    '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.

Yacoby
i thought that it will be intresting to create via php
diEcho
@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
@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
diEcho
@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
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
diEcho
+4  A: 

This might work..

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

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
so i have to put date ...always ??
diEcho
i have to open my php code daily and have to edit the date?
diEcho
You have an array of week days. The current date doesn't change the array in anyway. Why does it matter?
reko_t
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.
diEcho
@I Like PHP I see, self-made job security. I pity the developer who has to maintain your code eventually. ;P
deceze
A: 
$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
A: 

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
by your code,array always starts with monday
diEcho
Because today is Monday! But if you use $date = new DateTime('2010-03-28');, it starts from Sunday
Nicolò Martini