php 如何在php中创建一个工作日名称的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2536748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:52:58  来源:igfitidea点击:

how to create array of a week days name in php

php

提问by diEcho

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:

我知道这很愚蠢,但我该怎么做?我想通过PHP创建一个7天的数组我的意思是所有的7个工作日。我不想像他那样写:

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.

并且几天将从星期日开始,这意味着如果今天是 3 月 29 日(星期一),那么它会自动获取当前日期并从星期日开始创建一个工作日数组。

array always be in this way

数组总是这样

 $weakarray=("sunday","monday",......,"saturday");

回答by Thiago Belem

This might work..

这可能有效..

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

回答by Yacoby

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.

任何其他解决方案都会使您的代码更难理解,在这种情况下,动态地执行它似乎是矫枉过正。

回答by Rumen Tabakov

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

参见 PHP 的jddayofweek

回答by cheelahim

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

回答by codaddict

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

回答by reko_t

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

回答by edeoleo

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

}

回答by deceze

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

不过说真的,除非你的问题被误解了,否则我完全赞同 Yacoby 的回答。

回答by Nicolò Martini

use the DateTimeobject

使用DateTime对象

$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):

如果要从星期日开始,请使用星期日(例如,昨天)创建 DateTime:

$date = new DateTime('2010-03-28');

回答by ShapCyber

Reference TuiTalk answer here is how you can use it.

参考 TuiTalk 回答这里是如何使用它。

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
}

?>