-1

I want to change the way the number is produced, based on what the number value is. The default values are in unix epoch, I convert them to hours in echo.

$clock = time(); 

$sql = "SELECT unix FROM things";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
         echo round(($row["unix"] - $clock)/3600) .' hours from now.;

Right now it works, and it shows hours correctly, BUT i want to change it based on what unix timestamp shows, this is where it gets tricky. I want that The "hours" would be changed to minutes if the unix seconds are less than 3600, to seconds if it is less than 60, to days if it is more than 86400. How the hell do I do that?

Petras V.
  • 177
  • 1
  • 9
  • 1
    possible dupplicate : https://stackoverflow.com/questions/8273804/convert-seconds-into-days-hours-minutes-and-seconds – Moch. Rasyid Jun 14 '17 at 03:16
  • Possible duplicate of [Convert seconds into days, hours, minutes and seconds](https://stackoverflow.com/questions/8273804/convert-seconds-into-days-hours-minutes-and-seconds) – LF00 Jun 14 '17 at 03:36

2 Answers2

0

You can nest your little heart out.

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        //DONT NEED $ SIGN WHEN ROUNDING!!!!
        $time = round(($row["unix"] - $clock / 3600);
        if ($time <= 60) {
            echo $time . ' seconds from now.';
        } elseif (($time > 60) && ($time <= 3600)) {
            echo $time . ' hours from now.';
        } else {
            // You get the idea.
        }
    }
}
Petras V.
  • 177
  • 1
  • 9
Joe
  • 501
  • 3
  • 17
0
if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
    $time = round(($row["unix"] - $clock / 3600);
     if ($time <= 60) {
        echo  $time . ' seconds from now.';
     } elseif (($time > 60) && ($time <= 86400)) {
        echo $time . ' hours from now.';
     } else {
        echo  $time . ' days from now.';
    }
  }
}
Persson
  • 422
  • 4
  • 21