0

My code for recalling my table displays date and UTC time in 24 hour format. This is cool, however is there a way to get it to say "seconds ago", "minutes ago", "hours ago", "days ago". Rather than the exact date and time? Thanks in advance! UPDATED CODE: Still doesn't work, now fields don't populate either :(

<!DOCTYPE html>
 <html>
 <head>
 <link href='style.css?lol=<?php echo time(); ?>' rel='stylesheet' 
 type='text/css'>

 </head>
 <body>
     
      <script type='text/javascript'>
    setInterval(function() {
        var date = new Date();
        var fy = date.getUTCFullYear();
        var tm = date.getUTCMonth() + 1;
        var td = date.getUTCDate();
        var h = date.getUTCHours();
        var m = date.getUTCMinutes();
        var s = date.getSeconds();
        tm = checkTime(tm);
        td = checkTime(td);
        m = checkTime(m);
        s = checkTime(s);
        $('#time_id').html(fy + "-"+ tm + "-" + td + " " + h + ":" + m + ":" + s );
    }, 500);
    function checkTime(i) {
        if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
        return i;
    }
</script>


 <?php
 
    $curenttime = $table['posted_time'];
    $time_ago = strtotime($curenttime);
 
 function timeAgo($time_ago) {
    $cur_time = time();
    $time_elapsed = $cur_time - $time_ago;
    $seconds = $time_elapsed ;
    $minutes = round($time_elapsed / 60 );
    $hours = round($time_elapsed / 3600);
    $days = round($time_elapsed / 86400 );
    $weeks = round($time_elapsed / 604800);
    $months = round($time_elapsed / 2600640 );
    $years = round($time_elapsed / 31207680 );
    if ($seconds <= 60) {
        echo "just now";
    } else if ($minutes <= 60) {
        if ($minutes == 1) {
            echo "1 minute ago";
        } else {
            echo "$minutes"." minutes ago";
        }
    } else if ($hours <= 24) {
        if ($hours == 1) {
            echo "1 hour ago";
        } else {
            echo "$hours"." hours ago";
        }
    } else if ($days <= 7) {
        if ($days == 1) {
            echo "yesterday";
        } else {
            echo "$days"." days ago";
        }
    } else if ($weeks <= 4.3) {
        if ($weeks == 1) {
            echo "1 week ago";
        } else {
            echo "$weeks"." weeks ago";
        }
    } else if ($months <= 12) {
        if ($months == 1) {
            echo "1 month ago";
        } else {
            echo "$months"." months ago";
        }
    } else {
        if ($years == 1) {
            echo "1 year ago";
        } else {
            echo "$years"." years ago";
        }
    }
}

 include_once("dbConnect.php");
 if (isset($_GET["frompost"])) {
 echo "<h3>Thank you for your scrim post! It's been tweeted by <a 
 href='http://twitter.com/scrimfinder'>ScrimFinder</a>.";   
 echo "<br>";
 } else {
 }
 echo '<table cellspacing="0" cellpadding="10" width="1"><tr class="top">  
 <th>System</th>
 <th>Region</th>
 <th>Game</th>
 <th>Match Type</th>
 <th>Time Posted (UTC)</th>
 <th>Individual or Team?</th>
 <th>Gamertag</th>
 <th>Twitter</th>
 </tr>';  
 $sql = "SELECT * FROM scrims ORDER BY id DESC LIMIT 15;";  
 $result = mysqli_query($conn, $sql);  
 if(mysqli_num_rows($result)  > 0):
 while ($row = mysqli_fetch_array($result) ) {    
 echo '        
 <tr><td><center>'.$row["system"].'</center></td><td class="grey">
 <center>'.$row["region"].'</center></td>  <td>
 <center>'.$row["game"].'</center></td>  <td class="grey"> 
 <center>'.$row["matchtype"].'</center></td>  <td>
 <center>'.$row["timeAgo($time_ago)"].'</center></td>  <td class="grey">
 <center>'.$row["Ind_Team"].'</center></td>  <td class="grey">
 <center>'.$row["gamertag"].'</center></td>  <td><center><a 
 href="http://www.twitter.com/'.$row['twitter'].'" target="_blank"><input 
 type="submit" value="Message '.$row['twitter'].'" class="button" style="white-space:normal;"></a>
 </center></td>          </tr>  ';  }
 else:
 echo "<tr><td colspan = '100'>NO RECORDS FOUND</td></tr>";
 endif;
 echo '</table>';
 ?>
  • Did you try with momentJS library? – SilentCoder Feb 16 '18 at 19:42
  • Would momentJS play nicely with MySQL? I've never used that library... – Brady Pennington Feb 16 '18 at 19:44
  • Please use snippets **only** for code that is *fully executable* in the snippet and that can be used to *reproduce the problem or the problematic behaviour*. Do not use snippets for every code block, especially not for code in other languages than HTML, CSS and JS. You can mark some text block as code by selecting it and pressing `Ctrl+K` or by clicking this button: `</>` in the editor. – MEE Feb 16 '18 at 19:45
  • @BradyPennington yes it will support, It is a simple javascript library. – SilentCoder Feb 16 '18 at 19:46

2 Answers2

1

You can use momentJS and get the values as you mentioned in your questions.

moment("20111031", "YYYYMMDD").fromNow(); // 6 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 6 years ago
moment().startOf('day').fromNow();        // an hour ago
moment().endOf('day').fromNow();          // in a day
moment().startOf('hour').fromNow();       // 10 Minutes ago

According to the @MEE's comment and the PHP concerns, I suggests you to refer below links,

momantJS php library

php integration

SilentCoder
  • 1,970
  • 1
  • 16
  • 21
  • The OP is using PHP and not JavaScript. Please explain how the OP can apply this JavaScript library to his PHP output (of course the answer is not wrong) – MEE Feb 16 '18 at 19:47
  • That's what I was concerned about, php doesn't tend to play nice with JS libraries in my experience. However, I am open to utilizing it if it resolves my issue :P – Brady Pennington Feb 16 '18 at 19:48
  • 1
    Your concern is right. I'm sorry, I get little bit confuse. By the way please give it a try, By the way refer this link where you can have the php version of the moment library - https://github.com/fightbulc/moment.php. May be that will support – SilentCoder Feb 16 '18 at 19:52
  • may be this link also will help to you - https://stackoverflow.com/a/2916189/2820409 – SilentCoder Feb 16 '18 at 19:56
1

Important, this is the JS code I use to get the time

<textarea id="time_id"></textarea>

<script type='text/javascript'>
    setInterval(function() {
        var date = new Date();
        var fy = date.getUTCFullYear();
        var tm = date.getUTCMonth() + 1;
        var td = date.getUTCDate();
        var h = date.getUTCHours();
        var m = date.getUTCMinutes();
        var s = date.getSeconds();
        tm = checkTime(tm);
        td = checkTime(td);
        m = checkTime(m);
        s = checkTime(s);
        $('#time_id').html(fy + "-"+ tm + "-" + td + " " + h + ":" + m + ":" + s );
    }, 500);
    function checkTime(i) {
        if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
        return i;
    }
</script>

Then I use this PHP script to get the time from DB

I can't remember exactly where I got this code from so I can't give credit to author but I did find this online.

function timeAgo($time_ago) {
    $cur_time = time();
    $time_elapsed = $cur_time - $time_ago;
    $seconds = $time_elapsed ;
    $minutes = round($time_elapsed / 60 );
    $hours = round($time_elapsed / 3600);
    $days = round($time_elapsed / 86400 );
    $weeks = round($time_elapsed / 604800);
    $months = round($time_elapsed / 2600640 );
    $years = round($time_elapsed / 31207680 );
    if ($seconds <= 60) {
        echo "just now";
    } else if ($minutes <= 60) {
        if ($minutes == 1) {
            echo "1 minute ago";
        } else {
            echo "$minutes"." minutes ago";
        }
    } else if ($hours <= 24) {
        if ($hours == 1) {
            echo "1 hour ago";
        } else {
            echo "$hours"." hours ago";
        }
    } else if ($days <= 7) {
        if ($days == 1) {
            echo "yesterday";
        } else {
            echo "$days"." days ago";
        }
    } else if ($weeks <= 4.3) {
        if ($weeks == 1) {
            echo "1 week ago";
        } else {
            echo "$weeks"." weeks ago";
        }
    } else if ($months <= 12) {
        if ($months == 1) {
            echo "1 month ago";
        } else {
            echo "$months"." months ago";
        }
    } else {
        if ($years == 1) {
            echo "1 year ago";
        } else {
            echo "$years"." years ago";
        }
    }
}

You would then echo the time like:

<?php
    $curenttime = $table['date_time'];
    $time_ago = strtotime($curenttime);
    echo timeAgo($time_ago);
?>
JeanPaul98
  • 492
  • 6
  • 18