My code for reference:
$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);
I need the numbers stored within this array to be converted to weekday names, for example: 1 = Sunday
. I would also like to know if this can be done natively in PHP, or will it be necessary to use a library?
Advertisement
Answer
I think with “with everyone’s” you want to create the array with the names of the days of the week in a certain language. The IntlDateFormatter together with DateTime is the right class for this.
$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);
Output:
array ( 0 => 'lunes', 1 => 'martes', 2 => 'miércoles', 3 => 'jueves', 4 => 'viernes', 5 => 'sábado', 6 => 'domingo', )