Reputation: 54032
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
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
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
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
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
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
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
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
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
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
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
Reputation: 56430
$now = time();
$days = array();
for ($i = 0; $i < 7; $i++) {
$days[] = strftime('%A', $now);
$now += 60*60*24;
}
Upvotes: 1