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
Share on other sites

You want the date time functions to show today's date and time then less the field value. Depending on how accurately you want to return the date time will depend on what you use in the date function.

Link to comment
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.