如何将数字转换为星期几名称?
How to convert numbers to day of the week name?
我的参考代码:
$data1 = date('d-m-Y');
$array = array();
$i = 0;
while($i < 20){
$datagenerica = substr($data1,stripos($data1,'-'));
$datagenerica = $i.$datagenerica;
$data = date('w',strtotime($datagenerica));
$array[] = $data;
$i++;
}
$array = array_unique($array);
sort($array);
我需要将存储在此数组中的数字转换为工作日名称,例如:1 = Sunday
。我还想知道这是否可以在 PHP 中本地完成,或者是否需要使用库?
我认为对于“with everyone's”,您想使用某种语言创建包含星期几名称的数组。 IntlDateFormatter together with DateTime 是正确的 class。
$lang = 'es';
$IntlDateFormatter = new IntlDateFormatter(NULL,NULL,NULL);
$weekdays = [];
//Here you can specify with which day of the week the array should begin.
$date = date_create("last Monday");
for($i=0; $i<7;$i++){
$weekdays[] = $IntlDateFormatter->formatObject($date,"EEEE",$lang);
$date->modify("+1 Day");
}
//test output
echo '<pre>';
var_export($weekdays);
输出:
array (
0 => 'lunes',
1 => 'martes',
2 => 'miércoles',
3 => 'jueves',
4 => 'viernes',
5 => 'sábado',
6 => 'domingo',
)
我的参考代码:
$data1 = date('d-m-Y');
$array = array();
$i = 0;
while($i < 20){
$datagenerica = substr($data1,stripos($data1,'-'));
$datagenerica = $i.$datagenerica;
$data = date('w',strtotime($datagenerica));
$array[] = $data;
$i++;
}
$array = array_unique($array);
sort($array);
我需要将存储在此数组中的数字转换为工作日名称,例如:1 = Sunday
。我还想知道这是否可以在 PHP 中本地完成,或者是否需要使用库?
我认为对于“with everyone's”,您想使用某种语言创建包含星期几名称的数组。 IntlDateFormatter together with DateTime 是正确的 class。
$lang = 'es';
$IntlDateFormatter = new IntlDateFormatter(NULL,NULL,NULL);
$weekdays = [];
//Here you can specify with which day of the week the array should begin.
$date = date_create("last Monday");
for($i=0; $i<7;$i++){
$weekdays[] = $IntlDateFormatter->formatObject($date,"EEEE",$lang);
$date->modify("+1 Day");
}
//test output
echo '<pre>';
var_export($weekdays);
输出:
array (
0 => 'lunes',
1 => 'martes',
2 => 'miércoles',
3 => 'jueves',
4 => 'viernes',
5 => 'sábado',
6 => 'domingo',
)