variable hour interval php [closed]

0

I'd like to add this range variable to this code but I'm not getting it, if you can help me I'll be very grateful.

I found this code on the Internet and adapted to my system, but I ran into this issue of the range.

date_default_timezone_set("Brazil/East");
$con=@mysql_connect("localhost","root","");
$bd=mysql_select_db("test",$con);
//$hora_inicio = $_POST['hora_inicio'];  
//$hora_final = $_POST['hora_final'];
$hora_inicio = "07:00";  
$hora_final = "10:00";  
$int ="00:30";// variavel que define o intervalo           
$ini = strtotime($hora_inicio);
$fim = strtotime($hora_final);
$atu = $ini;
$i = 1;
for ($atu = $ini ;  $atu <= $fim; $atu = strtotime('+30 minutes', $atu)) {
    $hr_agendamento = date('H:i', $atu);
    $sql = mysql_query("INSERT INTO agenda (id_agenda,hr_agendamento) VALUES('','$hr_agendamento')");                        
}
echo "agenda criada";
    
asked by anonymous 24.08.2017 / 18:36

3 answers

0

Try this:

<?php
date_default_timezone_set("Brazil/East");
$con = mysqli_connect("localhost", "root", "");
$bd  = mysqli_select_db("test", $con);

$hora_inicio = new DateTime('07:00');
$hora_final  = new DateTime('10:00');

while($hora_inicio->add(new DateInterval('PT30M')) < $hora_final) {   
    $sql = mysql_query("INSERT INTO agenda(hr_agendamento) VALUES('".$hora_inicio->format('H:i')."')");                        
}

echo "Agenda criada!";
    
24.08.2017 / 19:07
0

To make the range dynamic, just use the variable with just the amount of minutes, like this:

$int ="30";// variavel que define o intervalo em minutos    

And in your For, you should use the variable $ int instead of "30", concatenating with +, like this:

for ($atu = $ini ;  $atu <= $fim; $atu = strtotime('+'.$int.' minutes', $atu)) {

Replace the indicated lines and take the test. Now you can change the value of the variable and it will generate the schedules in the desired interval.

All code:

date_default_timezone_set("Brazil/East");
$con=@mysql_connect("localhost","root","");
$bd=mysql_select_db("test",$con);
//$hora_inicio = $_POST['hora_inicio'];  
//$hora_final = $_POST['hora_final'];
$hora_inicio = "07:00";  
$hora_final = "10:00";  
$int ="30";// variavel que define o intervalo em minutos           
$ini = strtotime($hora_inicio);
$fim = strtotime($hora_final);
$atu = $ini;
$i = 1;
for ($atu = $ini ;  $atu <= $fim; $atu = strtotime('+'.$int.' minutes', $atu)) {
    $hr_agendamento = date('H:i', $atu);
    $sql = mysql_query("INSERT INTO agenda (id_agenda,hr_agendamento) VALUES('','$hr_agendamento')");                        
}
echo "agenda criada";
    
24.08.2017 / 19:09
0

You can simplify the creation of date and time periods with the class DatePeriod in the construction. It takes three arguments, the start of the period, the interval and the end.

If possible, migrate your code with the functions mysql_ to mysqli_ .

$inicio = new DateTime(@"07:00");
$fim = new DateTime(@"10:00");
$fim->modify('+30 minutes');

$periodo = new DatePeriod($inicio, new DateInterval('PT30M'), $fim);


foreach ($periodo as $item) {
    $sql = sprintf("INSERT INTO agenda (hr_agendamento) VALUES('%s')", $item->format('H:i'));
    mysql_query($sql) or print 'falha em: '. mysql_error();
}
    
24.08.2017 / 19:36