-2

I need an array (day, day of week and month) in php That itself contains the current week from Friday Until Wednesday of the following week.

Thank you very much for your help

With this script I get the array only the current week (starting Sunday or Monday) - nuevo to Spanish.

Do not get this working with TWO DIFFERENT pieces of TWO weeks

HELP please

// Name day week in Spanish
function diaespanol($valor){
$valor = strtotime($valor);
switch (date('w', $valor)){
case 0: $titleday ="Domingo"; break;
case 1: $titleday ="Lunes"; break;
case 2: $titleday ="Martes"; break;
case 3: $titleday ="Miercoles"; break;
case 4: $titleday ="Jueves"; break;
case 5: $titleday ="Viernes"; break;
case 6: $titleday ="Sabado"; break;
}

return $titleday ;

}

//name in Spanish of the month
function mesydia($valor){
$mes = substr($valor,5,+2);
$dia = substr($valor,8,+2);
switch ($mes){
case '01': $titulomes ="ene"; break;
case '02': $titulomes ="feb"; break;
case '03': $titulomes ="mar"; break;
case '04': $titulomes ="abr"; break;
case '05': $titulomes ="may"; break;
case '06': $titulomes ="jun"; break;
case '07': $titulomes ="jul"; break;
case '08': $titulomes ="ago"; break;
case '09': $titulomes ="sep"; break;
case '10': $titulomes ="oct"; break;
case '11': $titulomes ="nov"; break;
case '12': $titulomes ="dic"; break;
}   
return $dia.'/'.$titulomes; 
}

// current day
$dia = date('Y-m-d');

$diarecibido = $dia;

//Date of initial calendar day 7 days a week from Monday to Sunday
switch (date('w', $diasemana)){
case 0: $titleday ="Domingo"; 
$menos=6;
$iniciosemana = date("Y-m-d", strtotime("$diarecibido -$menos day"));
break;
case 1: $titleday ="Lunes"; 
$menos=1;
$iniciosemana = $diarecibido;
break;
case 2: $titleday ="Martes"; 
$menos=1;
$iniciosemana = date("Y-m-d", strtotime("$diarecibido -$menos day"));
break;
case 3: $titleday ="Miercoles"; 
$menos=2;
$iniciosemana = date("Y-m-d", strtotime("$diarecibido -$menos day"));
break;
case 4: $titleday ="Jueves"; 
$menos=3;
$iniciosemana = date("Y-m-d", strtotime("$diarecibido -$menos day"));
break;
case 5: $titleday ="Viernes"; 
$menos=4;
$iniciosemana = date("Y-m-d", strtotime("$diarecibido -$menos day"));
break;
case 6: $titleday ="Sabado"; 
$menos=5;
$iniciosemana = date("Y-m-d", strtotime("$diarecibido -$menos day"));
break;
}

//day of the week
$dia_semana = (date('w'));


//if today is not Sunday
if ($dia_semana != 0 ){
//muestra semana actual
for($i=0; $i<7; $i++){      
$mostrable = date("Y-m-d", strtotime("$iniciosemana +$i day"));
$titleday=diaespanol($mostrable);

$array_nombres_dias[] = diaespanol($mostrable);  
$array_fechas_dias[] = mesydia($mostrable);

}   
}
//if today is Sunday
else{   
//muestra semana siguiente
for ($i = 7; $i < 14; $i++) {

$mostrable =date("Y-m-d", strtotime("$iniciosemana +$i day"));
$titleday = diaespanol($mostrable);

if($titleday =='Sabado' || $titleday=='Domingo'){
$findesemana='style="background:#FF0033?';

}
else{
$findesemana= '';

}

$array_nombres_dias[] = $titleday;  
$array_fechas_dias[] = mesydia($mostrable); 

}

}

//I walk two arrays
$longitud = count($array_fechas_dias);
echo $longitud;

for($i=0; $i<$longitud; $i++)
{
//sack the value of each element
echo $array_fechas_dias[$i] . " - ";
echo $array_nombres_dias[$i];     
echo "<br>";
}
  • 2
    "Do it for me" is not really a type of a question that is welcome here. Also, it's a kinda duplicate of this http://stackoverflow.com/questions/13128854/php-datetime-class-change-first-day-of-the-week-to-monday – walther Jan 03 '15 at 13:12
  • Concept is to first find the first Friday. then find following Wednesday. then store this loop in an array and then loop the whole thing to the desired date. – Prashant Jan 03 '15 at 17:54

1 Answers1

0
<?php
// today
$startDate = date('Y-m-d');
$endDate = '2015-01-31';
// check the day today and find the next day you are interested in first
function nextDayByName($dayName="Sunday", $day='')
{
    if(true===empty($day))
    {
        $day = date('Y-m-d');
    }
    if(date("l") !== $dayName)
    {
        $dayTimestamp = strtotime("next $dayName", strtotime($day));
    } else {
        $dayTimestamp = strtotime($day);
    }
    return date('Y-m-d',$dayTimestamp);
}

function addDaysUntilNextCalendarDay($day, $dateTo)
{
    return nextDayByName($day, $dateTo);
}

$allInterestedDays = array();

do
{
    $startDate = nextDayByName("Friday", $startDate);
    $interateDays = $startDate;
    // echo $interateDays;
    // exit;
    $nextWednesday = addDaysUntilNextCalendarDay("Wednesday", $startDate);
    do 
    {
        $allInterestedDays[] = $interateDays;
        $interateDays = date('Y-m-d', strtotime($interateDays) + 86400);
    } while(strtotime($interateDays) <= strtotime($nextWednesday));

    //var_dump(array( "Friday" => $startDate, "nextWednesday" => $nextWednesday ));
} while($startDate <= $endDate && nextDayByName("Friday", $startDate) <= $endDate);

var_dump($allInterestedDays);

?>

I worked on a solution for you. Please do not ask other to work for you in future.

The function will provide you with all the days in a month between the Friday to Wednesday. I hope you can arrive to your solution from here on.

Also as a tip, its better to apply language translations as a php array replacements so you can include the language file you want to use for replacing from English (base language) to

Prashant
  • 2,005
  • 3
  • 17
  • 24