Pregunta:
Fecha: 11-04-2018 14:33:28
(En Español)
¿Cómo cambiar la hora de un array de fecha y hora según el día de la semana?[Resuelta]
Hola estoy tratando de poner en un array de fechas la hora a cero si son días sábdo o domingo. La idea és poner la hora según el día. No me funciona como la tengo, estaría agradecida de alguna ayuda para resolver esta problemática, desde ya muchas gracias
<head>
<style>
h4 { color: #FF0000; }
.sabado { color: #FF0000; }
.domingo { color: #FF0000; }
</style>
</head>
<body>
<?php
$start='2018-03-01';
$end='2018-03-31';
$listafechas=GetDays($start, $end);
echo "<pre>";
print_r($listafechas);
echo "</pre>";
for ($i = 0; $i < sizeof($listafechas); $i++) {
echo $listafechas[$i];echo "<br>";
$begintime= $listafechas[$i];
$endtime = gmdate("Y-m-d H:i", strtotime("+8 hours", strtotime($begintime)));
}
function GetDays($sStartDate, $sEndDate){
$sStartDate = gmdate("Y-m-d H:i", strtotime($sStartDate));
$sEndDate = gmdate("Y-m-d H:i", strtotime($sEndDate));
// Start the variable off with the start date
$aDays[] = $sStartDate;
// Set a 'temp' variable, sCurrentDate, with
// the start date - before beginning the loop
$sCurrentDate = $sStartDate; //2018-03-01
// While the current date is less than the end date
while($sCurrentDate < $sEndDate){
echo $sCurrentDate;
$fecha=$sCurrentDate;
$dia=date("w", strtotime($fecha));
// en caso que $dia sea sabado o domingo poner la hora de las fechas a cero..
if($dia=="0" or $dia=="6"){
echo "<br>";
echo "<p class=\"sabado\">$fecha</p>";
// echo "o es sabado o es domingo";
$sCurrentDate = gmdate("Y-m-d 00:00", strtotime("+1 day", strtotime($sCurrentDate)));
echo "<br>";
break;
}
// y sino poner la hora de las fechas a las 9:00
$sCurrentDate = gmdate("Y-m-d 09:00", strtotime("+1 day", strtotime($sCurrentDate)));
$aDays[] = $sCurrentDate;
}
return $aDays;
}
?>
</body>
</html>
Votos: 0 -
Respuestas: 4 -
Vistas: 14
Compartir en: Google
Facebook
Twitter
LinkedIn
Link
Respuestas:
-
Fecha: 11-04-2018 15:53:19 Hola que tal, prueba de esta manera:
function GetDays($tDay, $fDay){ $out = array();//iniciamos el Arreglo $dia = date("w", strtotime($tDay));//Verificamos el día si Sábado o Domingo ; //Validamos y agregamos la fecha formateada al arreglo para iniciarlo $ini = ($dia=="0" || $dia=="6") ? date("Y-m-d 00:00", strtotime($tDay)) : date("Y-m-d 09:00", strtotime($tDay)); array_push($out, $ini); //Sumamos el día he iniciamos el bucle $inicia = date("Y-m-d", strtotime("+1 day", strtotime($tDay))); //Recorremos las fechas while($inicia <= $fDay){ //Validamos de nuevo la fecha del bucle $valid = date("w", strtotime($inicia)); //Validamos el dñia de la semana $sCurrentDate = ($valid == "0" || $valid == "6") ? date("Y-m-d 00:00", strtotime($inicia)) : date("Y-m-d 09:00", strtotime($inicia)); //Agregamos al array array_push($out,$sCurrentDate); //Reiniciamos el dia //Sumamos 1 día a la fecha $suma = date("Y-m-d", strtotime("+1 day", strtotime($inicia))); $inicia = $suma; } //Retornamos el array return $out; }// $start = '2018-04-01'; $end = '2018-04-30'; $listafechas=GetDays($start, $end); foreach($listafechas as $item){ echo "<li>".date("l", strtotime($item))." - ".$item."</li>"; }
esto genera una lista así:
Sunday - 2018-04-01 00:00
Monday - 2018-04-02 09:00
Tuesday - 2018-04-03 09:00
Wednesday - 2018-04-04 09:00
Thursday - 2018-04-05 09:00
Friday - 2018-04-06 09:00
Saturday - 2018-04-07 00:00
Sunday - 2018-04-08 00:00
Monday - 2018-04-09 09:00
Tuesday - 2018-04-10 09:00
etc... Votos: 2 - Link respuesta -
Fecha: 12-04-2018 00:34:18 Hola Carlos Quintero, muchísimas gracias. Eso era lo que necesitaba.Realmente buena solución, y por sobretodo una solución!! Gracias, estoy bien, espero que tu también. Votos: 1 - Link respuesta
-
Fecha: 13-04-2018 20:22:19 Me encanto tu pregunta Maria, y la use como un ejercicio aqui postea tambien una respuesta
<?php $start='2018-04-01'; $end=30; $arr=array(); $ini=1; while($ini<=$end) { $date = new DateTime("2018-04-$ini 03:00"); $timestamp= $date->getTimestamp(); $dayname=getdate($timestamp); if($dayname['weekday']=='Saturday' or $dayname['weekday']=='Sunday' ){ $date = new DateTime("2018-03-$ini 00:00"); } $arr[]=$date->format('Y-m-d H:i:s'); //echo $date->format('Y-m-d H:i:s') . "<br><br>"; $ini++; } foreach($arr as $key=>$value){ echo "$key=>$value<br>"; } ?>
Resultado
0=>2018-03-01 00:00:00 1=>2018-04-02 03:00:00 2=>2018-04-03 03:00:00 3=>2018-04-04 03:00:00 4=>2018-04-05 03:00:00 5=>2018-04-06 03:00:00 6=>2018-03-07 00:00:00 7=>2018-03-08 00:00:00 8=>2018-04-09 03:00:00 9=>2018-04-10 03:00:00 10=>2018-04-11 03:00:00 11=>2018-04-12 03:00:00 12=>2018-04-13 03:00:00 13=>2018-03-14 00:00:00 14=>2018-03-15 00:00:00 15=>2018-04-16 03:00:00 16=>2018-04-17 03:00:00 17=>2018-04-18 03:00:00 18=>2018-04-19 03:00:00 19=>2018-04-20 03:00:00 20=>2018-03-21 00:00:00 21=>2018-03-22 00:00:00 22=>2018-04-23 03:00:00 23=>2018-04-24 03:00:00 24=>2018-04-25 03:00:00 25=>2018-04-26 03:00:00 26=>2018-04-27 03:00:00 27=>2018-03-28 00:00:00 28=>2018-03-29 00:00:00 29=>2018-04-30 03:00:00
Votos: 1 - Link respuesta -
Fecha: 14-04-2018 04:11:41 Muchas gracias por el aporte y comentario, Sr. Rodriguez. Lo estuve mirando y me gustó mucho también le veo uso.
Atte. Ulloa Votos: 0 - Link respuesta
Para participar activamente de la comunidad primero debes autenticarte, ingresa al sistema.Iniciar Sesión
