Schedule grid with PHP

Asked

Viewed 368 times

0

I’m having a little problem with my code, which generates schedule, it’s not entering the last time.

type 07:00 to 10:00 30 min interval but it only goes until 9:30 does not record the 10:00

This is my 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";             
$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";
?>
  • Your code didn’t come... post the code so we can help

  • <? date_default_timezone_set("Brazil/East"); $con=@mysql_connect("localhost","root",""); $bd=mysql_select_db("test",$con); $hora_start = "07:00"; $hora_end = "10:00"; $ini = strtotime(hora_start);&$Xa;$end = strtotime($hora_end); $atu = $ini; $i = 1; for ($atu = $ini ; $atu < $fim; $atu = strtotime('+30 minutes', $atu)) { $hr_scheduling = date('H:i', $atu); $sql = mysql_query("INSERT INTO agenda (id_agenda,hr_scheduling) VALUES(','$hr_scheduling')"); }

1 answer

0


This is happening because you’re comparing in your for in your line 13, if $atu is less than $fim.

for ($atu = $ini ; $atu < $end; $atu = strtotime('+30 minutes', $atu))

In order for him to do what you want, you must also compare if $atu is equal to $fim.

for ($atu = $ini ; $atu <= $end; $atu = strtotime('+30 minutes', $atu))

Getting your loop like this:

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";             
$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";

You can see running here on ideone.

  • It really worked... now I’m trying to change the time interval(+30) for a variable but it’s not working. has how to explain to me , why I wanted to pass this value via post also but not passing it generates an infinite loop or almost

  • Yes I can, so open a new question with detail and code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.