Using $ time = date ("d / m / Y H: i: s", time () - 86400); to delete past schedules

2

I am using this code to exclude schedules with dates and times already passed in relation to the current time.

<?php
while($res = mysql_fetch_array($select_agenda)){

$id = $res['id'];
$nome = $res['nome'];
$tel = $res['tel'];
$cel = $res['cel'];
$email = $res['email'];
$plano = $res['plano'];
$data = $res['data'];
$horas = $res['horas'];
$tempo = date("d/m/Y  H:i:s",time()-86400);

$sql = mysql_query("DELETE FROM agendar WHERE data < '$tempo'");
?>

Knowing that 86400 refers to the number of seconds of a day, I'm using the above code, and excluding the past schedules for the current time, at the turn of dates.

Following this reasoning, I changed it to -1800, so that the files were deleted every 30 minutes. Staying like this:

$tempo = date("d/m/Y  H:i:s",time()-1800);
$sql = mysql_query("DELETE FROM agendar WHERE data < '$tempo'");

But in this way you are deleting all records, not the last 30 minutes of real time.

Schedules are made at 30-minute intervals.

If friends can help me with how to proceed so that the schedules are deleted every 30 minutes, I'll be very grateful.

A big hug to everyone.

    
asked by anonymous 02.10.2015 / 20:31

2 answers

1

I do not know if I understand the question well, but you can delete the records with a datetime field with values less than 30 minutes from the current time using the MySQL NOW function ():

$sql = mysql_query("DELETE FROM agendar WHERE data <  NOW() - INTERVAL 30 MINUTE");

Remembering that NOW() returns the current date / time, and with INTERVAL can add (using + ) or subtract (using - ) from the current time (ie, the value returned by NOW() ).

Where I used MINUTE , you can replace with: YEAR for year, MONTH for month, DAY for day, HOUR to hour, MINUTE to minute, SECOND to second.

NOTE: Remember that LIB mysql is deprecated for versions from PHP 5.5 , use PDO or MySQLi .

I hope I have helped!

    
02.10.2015 / 20:50
1

To subtract 30 minutes from your initial PHP time, instead of $tempo = date("d/m/Y H:i:s",time()-1800); you can do:

$tempoInicial = date("d/m/Y  H:i:s",time());
$menosTrintaMinutos = date("Y-m-d H:i:s", strtotime("-30 minutes", strtotime($tempoInicial )));

//echo 'tempo inicial: '.$tempoInicial.'<br/>';
//echo 'tempo -30m: '.$menosTrintaMinutos;

$sql = mysql_query("DELETE FROM agendar WHERE data < '$menosTrintaMinutos '");

Executing $sql deletes all records in the agendar table where there is more than 30 minutes passed (when the data column is less than the current date, discounting 30 minutes).

    
02.10.2015 / 20:53