开发者

how to create array of a week days name in php

I know this is stupid, but how would I do this? 开发者_如何学CI 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");


This might work..

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


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.


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


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"
}


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)
);


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


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];

}


$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.


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');


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
}

?>


$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"));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜