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
2010-03-29 09:05:02
i said array should be alwayz started from sunday.....
diEcho
2010-03-29 09:06:49
then why do you want to create the array dynamically..? it's just extra overhead.
reko_t
2010-03-29 09:11:55
+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
2010-03-29 09:08:24
@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
2010-03-29 09:16:14
@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
2010-03-29 09:22:00
@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
2010-03-29 09:29:37
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
2010-03-29 09:32:17
+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
2010-03-29 09:10:09
+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
2010-03-29 09:11:37
You have an array of week days. The current date doesn't change the array in anyway. Why does it matter?
reko_t
2010-03-29 09:16:35
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
2010-03-29 09:19:25
@I Like PHP I see, self-made job security. I pity the developer who has to maintain your code eventually. ;P
deceze
2010-03-29 09:21:13
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
2010-03-29 09:13:50
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
2010-03-29 09:14:04
Because today is Monday! But if you use $date = new DateTime('2010-03-28');, it starts from Sunday
Nicolò Martini
2010-03-29 10:17:22