0
I am wanting 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 day 31/03, when it was to show from day 01/04 onwards... strange that with the other days works normal... anyone has any tips? Grateful!
Goal I wish is a JSON of this format:
USER INPUT
MES: 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 ask for forgiveness from friends because I really had not been "understandable" =) I think I have now made it clear, 1, 2 ... is the number of the week in the month. And I need the list of days, as described in JSON
Couldn’t figure out the goal... Is it to do the seven days including the inserted day? How many days are you supposed to return? You must be more specific in your goal
– Pedro Martins
As @Pedromartins said, you need to be more specific in what you want to present... contextualize and indicate the goal please.
– Fernando
I arranged @Pedromartins and Fernando, grateful
– Hermus
@Hermus let me know if there are any mistakes, okay? Hug!
– Andrei Coelho
@Hermus would you have any comment to make about my reply? You managed to test it?
– Andrei Coelho
@Andreicoelho was traveling, sorry for the delay, was show, was not exactly what I thought, but met my need. Grateful
– Hermus
@Hermus if you need any more help, let me know here. Hug!
– Andrei Coelho
@Hermus if I have to change anything I’ll help you.
– Andrei Coelho