variable interval php time

Asked

Viewed 270 times

0

I would like to add this variable interval to this code but I am not getting it , if you can help me I will be very grateful.

I found this code on the Internet and adapted it to my system, but I bumped into this issue of interval.

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";
  • even so the edited question do not quite know what you want to insert with a time interval ?

  • needs to be clearer and more specific in what you want

  • 5

    Possible duplicate of Schedule grid with PHP

  • I think it’s different because I want to add the interval. but in fact it’s the same code.

3 answers

0

You can simplify creating date or time periods with the class DatePeriod in the construction are necessary three arguments the beginning of the period, the interval and the end.

If possible migrate your code with the functions mysql_ to the 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();
}

0


Try it this way:

<?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!";

0

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

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

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

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

Replace the given lines and do the test. Now you can change the value of the variable and will generate the times in the desired range.

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

Browser other questions tagged

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