0

I want to display when the latest 'reactie' has been added. I think there is a simple php solution for that but I am not really finding it.

enter image description here

As you can see in the database there is a timestamp set too when a reaction has been added. now I want to display the time when the last reaction has been added.

Dave
  • 3,073
  • 7
  • 20
  • 33
  • "Now I want to display the time when the last reaction has been added." Without basing the last reaction time on the columns memberpage_id (ledenpagina_id), topic_id (onderwerp_id) or customer_id (klant_id) ? off topic p.s mixing your native language (dutch) for table name and some table columns with english programming language is clumsy.. – Raymond Nijland Apr 04 '18 at 12:11
  • 1
    You should definitely add some more details, including code. Please check here on how to ask good questions: https://stackoverflow.com/help/how-to-ask – rainer Apr 04 '18 at 12:31

2 Answers2

0

You can do:

select max(create_time)
from reacties;

If you want the whole row:

select r.*
from reacties r
order by create_time desc
limit 1;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Try this code, it shall do the work.

You just have to add you Database credentials.

https://www.w3schools.com/php/php_mysql_select.asp

This is a PHP function to convert the timestamp into the "ago" format use it combination with the MySQLi Query. Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago…

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT reactie, create_time FROM reacties ORDER BY create_time DESC LIMIT 1";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Reactie: ".$row['reatie']." Timestamp: "+time_elapsed_string($row['create_time']);
    }
} else {
    echo "0 results";
}
$conn->close();
?>
glenmyles
  • 13
  • 4
  • "now I want to display the time when the last reaction has been added." i don't see how and why this code should work like the topicstarter wanted.. Because you print out all records.. – Raymond Nijland Apr 04 '18 at 12:04
  • Yeah rigth, he then should order and limit it, but at least its a push in the right direction, also he didnt clearly state how he'd like to display it so i was just assuming, sorry... – glenmyles Apr 04 '18 at 12:09
  • Well I want to show the time between the last reaction and now like this: " 8 days ago". – questionsaretobeanswered Apr 04 '18 at 12:16
  • Have a look at it now, is this the way you wanted it? Did you even want to use PHP? – glenmyles Apr 05 '18 at 15:17