Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created March 18, 2019 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/140fb3aab79c8584372079823f1f380d to your computer and use it in GitHub Desktop.
Save parzibyte/140fb3aab79c8584372079823f1f380d to your computer and use it in GitHub Desktop.
<?php
/*
Hacer ciclos con fechas para ir desde un período
a otro, iterando hora a hora usando strtotime y date
@author parzibyte
*/
# Definir rangos de fechas.
# Nota: podríamos definirlas sin horas, pero es mejor ser explícitos
$fechaInicio = "2019-03-18 00:00:00";
$fechaFin = "2019-03-21 00:00:00";
# Fecha como segundos
$tiempoInicio = strtotime($fechaInicio);
$tiempoFin = strtotime($fechaFin);
#60 minutos por hora * 60 segundos por minuto
$hora = 3600;
while($tiempoInicio <= $tiempoFin){
# Podemos recuperar la fecha actual y formatearla
# Más información: http://php.net/manual/es/function.date.php
$fechaActual = date("Y-m-d H:i:s", $tiempoInicio);
printf("Fecha y hora dentro del ciclo: %s\n", $fechaActual);
# Sumar el incremento para que en algún momento termine el ciclo
$tiempoInicio += $hora;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment