Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

round down to nearest 5 mintues php mysqli

I"m trying to get this time code to round down to the nearest time example if the time listed is 10:28, then i want it to show 10:25

What I have is (the time modify is required)

<?php 
include('../../database/2ndconnection.php');

$sql="SELECT * FROM raid ORDER BY TimeRecord DESC LIMIT 1";
$result=mysqli_query($dbcon,$sql);

while($rows=mysqli_fetch_array($result)){
    $dateStr = $rows['TimeRecord'];

    $pst = new \DateTime($dateStr);
    $pst->modify("-3 hours");

    $est = new \DateTime($dateStr);
} ?>

PST: <?php echo $pst->format('H:i'); ?>
<br>EST: <?php echo $est->format('H:i'); ?>

thanks in advance for your help

like image 716
JoJo Avatar asked Dec 04 '25 18:12

JoJo


1 Answers

Insert this:

<?php
$currentMinutes = $time->format('i');
$minutesToSubtract = $currentMinutes % 5;
$time = $time->sub(new DateInterval('M' . $minutesToSubtract));
?>

$currentMinutes will hold the current number of minutes, using %, you can compute the minutes that need to be subtracted, and afterwards you subtract them through the sub function

It could be integrated into your code as following:

<?php 
include('../../database/2ndconnection.php');

$sql="SELECT * FROM raid ORDER BY TimeRecord DESC LIMIT 1";
$result=mysqli_query($dbcon,$sql);

while($rows=mysqli_fetch_array($result)){
    $dateStr = $rows['TimeRecord'];

    $pst = new \DateTime($dateStr);
    $pst->modify("-3 hours");

    $est = new \DateTime($dateStr);
}

$currentMinutes = $pst->format('i');
$minutesToSubtract = $currentMinutes % 5;
$pst = $pst->sub(new DateInterval('M' . $minutesToSubtract));

?>

PST: <?php echo $pst->format('H:i'); ?>
<br>EST: <?php echo $est->format('H:i'); ?>
like image 163
Nico Haase Avatar answered Dec 07 '25 09:12

Nico Haase



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!