I want to print the days of the week separated by the number of the week of a given month: What I have so far is:
function days_week($date = NULL){
$date = ($date == NULL) ? date('m/d/Y') : $date;
$ts = strtotime($date);
$year = date('o', $ts);
$week = date('W', $ts);
$return = [];
for($i = 0; $i <= 6; ++$i) {
$ts = strtotime($year.'W'.$week.$i);
$week_month = date('w', strtotime($ts));
$return[$week_month][$i]['day_week'] = date("d/m/Y", $ts);
//echo $return['day_week'];
switch(date("l", $ts)){
case 'Sunday':
$return[$week_month][$i]['day_name'] = "domingo";
break;
case 'Monday':
$return[$week_month][$i]['day_name'] = "segunda";
break;
case 'Tuesday':
$return[$week_month][$i]['day_name'] = "terça";
break;
case 'Wednesday':
$return[$week_month][$i]['day_name'] = "quarta";
break;
case 'Thursday':
$return[$week_month][$i]['day_name'] = "quinta";
break;
case 'Friday':
$return[$week_month][$i]['day_name'] = "sexta";
break;
case 'Saturday':
$return[$week_month][$i]['day_name'] = "sábado";
break;
};
//echo " - ".$return['day_name'];
switch(date("M", $ts)){
case 'Jan':
$return[$week_month][$i]['month_name'] = 'janeiro';
break;
case 'Feb':
$return[$week_month][$i]['month_name'] = 'fevereiro';
break;
case 'Mar':
$return[$week_month][$i]['month_name'] = 'março';
break;
case 'Apr':
$return[$week_month][$i]['month_name'] = 'abril';
break;
case 'May':
$return[$week_month][$i]['month_name'] = 'maio';
break;
case 'Jun':
$return[$week_month][$i]['month_name'] = 'junho';
break;
case 'Jul':
$return[$week_month][$i]['month_name'] = 'julho';
break;
case 'Aug':
$return[$week_month][$i]['month_name'] = 'agosto';
break;
case 'Sep':
$return[$week_month][$i]['month_name'] = 'setembro';
break;
case 'Oct':
$return[$week_month][$i]['month_name'] = 'outubro';
break;
case 'Nov':
$return[$week_month][$i]['month_name'] = 'novembro';
break;
case 'Dec':
$return[$week_month][$i]['month_name'] = 'dezembro';
break;
}
}
return json_encode($return);
}
Using:
echo "<pre>";
print_r(days_week("04/01/2018"));
echo "</pre>";
Returns the following:
{
"4": [
{
"day_week": "25/03/2018",
"day_name": "domingo",
"month_name": "março"
},
{
"day_week": "26/03/2018",
"day_name": "segunda",
"month_name": "março"
},
{
"day_week": "27/03/2018",
"day_name": "terça",
"month_name": "março"
},
{
"day_week": "28/03/2018",
"day_name": "quarta",
"month_name": "março"
},
{
"day_week": "29/03/2018",
"day_name": "quinta",
"month_name": "março"
},
{
"day_week": "30/03/2018",
"day_name": "sexta",
"month_name": "março"
},
{
"day_week": "31/03/2018",
"day_name": "sábado",
"month_name": "março"
}
]
}
But you can see that the result is wrong, because it shows the result until 31/03, when it was to show from 01/04 onwards ... strange that with other days it works normal ... someone Do you have any tips? Thanks!
Goal I want is a JSON of this format:
USER ENTRY
MONTH: 04 YEAR: 2018
OUTPUT (JSON):
{
"1": [
{
"day_week": "01/04/2018",
"day_name": "domingo",
"month_name": "abril"
},
{
"day_week": "02/04/2018",
"day_name": "segunda",
"month_name": "abril"
},
{
"day_week": "03/04/2018",
"day_name": "terça",
"month_name": "abril"
},
...
...
{
"day_week": "07/04/2018",
"day_name": "sabado",
"month_name": "abril"
}
],
"2": [
{
"day_week": "08/04/2018",
"day_name": "domingo",
"month_name": "abril"
},
{
"day_week": "09/04/2018",
"day_name": "segunda",
"month_name": "abril"
},
...................
I apologize to friends because it really was not "understandable" =) I believe you have now given to understand, the 1, 2 ... is the number of the week in the month. And I need the list of days, as described in JSON