Weborum Webmaster Forum > comparing dates
Help - Search - Members - Calendar
Full Version: comparing dates
Weborum Webmaster Forum > Web Page Design > PHP
Waleed
From a database, the contents of a field named user_lastvisit are displayed onto a page. We need to compare that date from the database with the current date and then display something like, "last login was x days ago."

For example, if the date in the database is 8/7/2006, and current date is 28/7/2006 then the message displayed would be "last login was 20 days ago"

Is there any way to do this?
Waleed
I was thinking maybe with Javascript? Anyone? goatee.gif
Joe
This may help you mate;

http://www.developertutorials.com/tutorial...1018/page1.html
xanderman
use time()

CODE
<?php
$now = time();
$lastlogin = //select last login time from the database
$timesincelast = $now - $lastlogin;
//note this is the number of time in seconds
//so dividing this number by 60 will give you mins.
//dividing by 60 again will give you hours.
//dividing again by 24 will give u days
$timelast = (($timesince / 60) / 60) / 24);
echo "$timelast days";
?>

Waleed
Thanks, xander!

I'm using this right now:

CODE
//DATE COMPARE - [http://www.php.net/manual/en/function.fmod.php#52740]
$timesince = time() - $row['user_lastvisit']; // current time minus user_lastvisit equals seconds since last visit

$daysarray = explode(".", ($timesince / 86400));
$days = $daysarray[0];

$partdays = fmod($timesince, 86400);
$hoursarray = explode(".", ($partdays / 3600));
$hours = $hoursarray[0];

$parthours = fmod($partdays, 3600);
$minarray = explode(".", ($parthours / 60));
$min = $minarray[0];

// to display seconds: $sec = fmod($parthours, 60);

//END DATE COMPARE

echo $days . ' days, ' . $hours . ' hours and ' . $min . ' minutes ago';}

This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.