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

Asked

Viewed 427 times

2

I am using this code to delete schedules with dates and times already past 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 am using the code above, and excluding the past schedules referring to the current time, at the turn of the dates.

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

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

But this way it is excluding all records, and not the last 30 minutes referring to real time.

Scheduling is done at 30-minute intervals.

If friends can give me a help on how I should proceed to schedules being deleted every 30 minutes, I would be very grateful.

Big hug to all.

2 answers

1

I don’t know if I understood the question correctly, but it is possible to delete the records with a field type "datetime" with values less than 30 minutes of the current time using the exclusion query a 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 the INTERVAL can add (using +) or subtract (using -) of the current time (i.e., of the value returned by NOW()).

Where I used MINUTE, it is possible to replace with: YEAR to year, MONTH to month, DAY to day, HOUR for the time, MINUTE for minute, SECOND for second.

NOTE: it is worth remembering that the mysql LIB is deprecated for versions starting with PHP 5.5 , utilize PDO or Mysqli.

I hope I’ve helped!

  • Hello Allan, thank you so much for your attention to my problem. But I am using a Datepicker where I select the date and write in a column Varchar and not in Datetime, and a Select where I select the times and save in a column also in Varchar, have you show me how to change my code to work with your tip? Hugs.

  • You could concatenate the value of Datepicker and Select to mount a datetime field and write to a suitable field. It would be very practical to use the date as an object, also for sorting with filters as > higher or < lower. What do you think?

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

Running $sql you delete all records from the table agendar where there are more than 30 minutes passed (when the column data is less than the current date minus 30 minutes).

  • This actually has little to see answer and does not answer. It seems that you have not read the question. On Soen this would be flagged for removal.

  • @felipsmartins, I made a point of editing my answer to be clearer. Previously I had only shown a path, I decided to complete the code to be more specific. It seems you didn’t read my answer (:

  • Hello feresjorge, I would like to thank you for your attention, but I replaced the lines $tempo = date("d/m/Y H:i:s",time()-1800); and $sql = mysql_query("DELETE FROM agendar WHERE data < '$tempo'"); for $tempoInicial = date("d/m/Y H:i:s",time()); , $menosTrintaMinutos = date("Y-m-d H:i:s", strtotime("-30 minutes", strtotime($tempoInicial))); and the sql for $sql = mysql_query("DELETE FROM agendar WHERE data < '$menosTrintaMinutos'");, but keeps deleting all records. Do you have any idea where I’m going wrong? Hugs.

Browser other questions tagged

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