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: 636
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.
User avatar
alice2019
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Dec 05, 2019 10:25 pm

Fri Dec 06, 2019 12:25 am

<?php
/*

*If you still don't understand, reply me, I am in China, daytime online
*from China>Jiangsu Province> Nanjing
*张兰兰2019-12-6
echo "T:- " . date("h:i:s",$day_elapsed) . "<br>";
This sentence is wrong. Correct it as

echo "T:- You haven't been online for" . date("h:i:s",$datediff) . "<br>";


if($day_elapsed>=1){
echo "You haven't been online for {$day_elapsed} day";
}else{
echo "Welcome back";
}



The first thing you should do is make sure that the value that the database pulls out is a string that represents the time.
1. $day_elapsed is a multiple of the number of days you have not been online for a few days.
2. If you want to calculate how many hours you have been offline, you should write $day_elapsed =($datediff/(60 * 60));It's a multiple of hours.
3. Finally, you can't write this $day_elapsed as time because it's a quotient, a multiple of days or hours, not a timestamp.
4. You can write $datediff as a time, because it is a timestamp difference, indicating a specific time and second when you are not online.

for example
*/

$row['reg_date']="2019-11-23";
//make sure $row['reg_date'] is time;
$now = time();
$your_date = strtotime($row['reg_date']);
$datediff = $now - $your_date;
$day_elapsed =($datediff / (60 * 60 * 24));
//$day_elapsed =($datediff / (60 * 60));

echo "C:- " . date("h:i:s",$row['reg_date']) . "<br>";
echo "T:- You haven't been online for" . date("h:i:s",$datediff) . "<br>";
if($day_elapsed>=1){
echo "You haven't been online for {$day_elapsed} day";
}else{
echo "Welcome back";
}


?>
User avatar
alice2019
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Dec 05, 2019 10:25 pm

Fri Dec 06, 2019 12:36 am

echo "T:- You haven't been online for" . date("h:i:s",$datediff) . "<br>";
NOT
echo "T:- " . date("h:i:s",$day_elapsed) . "<br>";
Post Reply