How do I calculate Time Please? ie take the timestamp and subtract the current time

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
IChaps
php-forum Fan User
php-forum Fan User
Posts: 131
Joined: Tue Mar 01, 2016 3:07 pm

Tue Oct 01, 2019 5:02 am

Hello.

Could anyone please advise me with regards writing a piece of code, which would check if a user has used their account in the past hour, and if not. Mark them as offline.

At present, I'm unsure where I've gone wrong. And to get the timestamp, and the current time, then take one away from the other.

When I run the code below, all I'm getting is 'C:- 01:33:39am T:- 01:00:02'
C being from the database and T been the results

Code: Select all

<?php
if(session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
$servername = $_SESSION['dbf_host'];
$username = $_SESSION['dbf_user'];
$password = $_SESSION['dbf_pass'];
$dbname = $_SESSION['dbf_databasename'];

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $results = $conn->query('SELECT * FROM client');
    while ($row = $results->fetch()) {
        $now = time();
        $your_date = strtotime($row['reg_date']);
        $datediff = $now - $your_date;
        $day_elapsed =($datediff / (60 * 60 * 24));

        echo "C:- " .  date("h:i:sa",$row['reg_date']) . "<br>";
        echo "T:- " .  date("h:i:s",$day_elapsed) . "<br>";
    }
}catch(PDOException $e) {}
?>
Could anyone please advise?
Thank You.
chorn
php-forum GURU
php-forum GURU
Posts: 608
Joined: Fri Apr 01, 2016 2:18 am

Tue Oct 01, 2019 10:05 pm

you could do that all in database. get an overview of matching clients

http://www.sqlfiddle.com/#!17/409a6/1

and use this as a base for your update query
IChaps
php-forum Fan User
php-forum Fan User
Posts: 131
Joined: Tue Mar 01, 2016 3:07 pm

Thu Oct 03, 2019 7:24 am

Hello chorn.

Thank you very much for the link. I'll give it a try.
Post Reply