Jump to content

DATETIME


Daney11

Recommended Posts

Hey guys

 

In my database i have a datetime field that shows, for example, "2008-03-18 20:00:00" that being the 18th March.

 

And each user has one of the these datetime fields with Last Logged in.

 

Im not sure on how to make it so It shows how long ago, like "Last Logged in: "2008-03-18 20:00:00" (2 hours ago)"

 

Any ideas?

Thanks

Link to comment
https://forums.phpfreaks.com/topic/96676-datetime/
Share on other sites

<?php

$date = strtotime(date("Y-m-d H:i:s")); // Converts today now, to seconds
$lastloggedin = strtotime("2008-03-17 21:00:00"); //converts then to seconds

$diff = $date - $lastloggedin;
?>

 

This will give you the difference in seconds which you can then use further code to produce a difference in hours, minutes, days etc.

Link to comment
https://forums.phpfreaks.com/topic/96676-datetime/#findComment-494727
Share on other sites

Im upto

 

$Week = '604800';
$Day = '86400';
$Hour = '3600';
$Minute = '60';
$Second = '1';

$date = strtotime(date("Y-m-d H:i:s"));
$lastloggedin = strtotime("2008-03-07 08:00:00");

$datea = ($date - $lastloggedin);

if ($datea >= $Week) { echo "1 Week Ago!"; }
elseif ($datea >= $Day) { echo "1 Day Ago!"; }
elseif ($datea >= $Hour) { echo "1 Hour Ago!"; }
elseif ($datea >= $Minute) { echo "1 Minute Ago!"; }
elseif ($datea >= $Second) { echo "1 Second Ago!"; }

 

Is there any code out there that would genereate

 

Last logged in: 1 Hour 2 minutes 34seconds ago

 

etc?

Link to comment
https://forums.phpfreaks.com/topic/96676-datetime/#findComment-494754
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.