Current location: Hot Scripts Forums » General Community » Script Requests » Difference between two dates


Difference between two dates

Reply
  #1 (permalink)  
Old 05-09-06, 02:06 AM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
Difference between two dates

hi, can someone provide me with a script to know the difference between two dates ? i searched everywhere but the info was vague. the problem is like this: i have a table which stores the date of the last login by user. i want this script to show after exactly how many days is the user loggin in(since his last login). please help me ! thanks in advance
Reply With Quote
  #2 (permalink)  
Old 05-09-06, 12:21 PM
Christian's Avatar
Christian Christian is offline
Community VIP
 
Join Date: Mar 2005
Location: ProgrammingTalk
Posts: 2,449
Thanks: 0
Thanked 6 Times in 5 Posts
Moved to Script Requests ....
__________________
:: ImperialBB :: New version in the works! :: http://www.imperialbb.com ::

:: Have a question about the board? The Rules? An Infraction/Warning? :: Contact Form ::
Reply With Quote
  #3 (permalink)  
Old 05-09-06, 02:17 PM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
I typed this up a while ago. Should work for you if you can convert the dates to a timestamp. If you don't have the timestamp available, you can convert pretty much any date format to a timestamp with http://www.php.net/mktime

Both parameters are UNIX timestamps. The first param is obviously the current time. The second param is the original recorded time you're comparing it to... so the first param will be larger than the second. If the first param is smaller, it will prepend a hyphen (negative sign).

It will display from years all the way down to seconds. It will display an increment of time only if it is greater than zero, so it will return something like
Code:
3 weeks, 2 days, 6 hours, 10 minutes, 34 seconds
So if the difference is 3 seconds more than a week, it will return only
Code:
1 week, 3 seconds
meaning it will NOT display useless data like
Code:
1 week, 0 days, 0 hours, 0 minutes, 3 seconds
If you don't want it to display too meticulously (like all the way down to seconds or minutes), just comment out or delete the blocks you don't want displayed (between their respective "Start" and "End" comments).

PHP Code:

<?php


function CalcDiff($currTime$origTime)
{
    
$showDiff '';

    
// Set times
    
$currTime intval($currTime);
    
$origTime intval($origTime);
    if (
$currTime<$origTime) { $diff $origTime-$currTime; }
    else { 
$diff $currTime-$origTime; }

    
// Start Years
    
$yrs floor($diff/31556926); // 31556926 secs/yr
    
if ($yrs 0)
    {
        
$diff $diff - ($yrs*31556926);
        
$showDiff .= "{$yrs} year";
        
$showDiff .= $yrs>'s' '';
    }
    unset(
$yrs); // End Years

    // Start Months
    
$mos floor($difference/2629743.83); // 2629743.83 secs/mo
    
if ($mos 0)
    {
        
$diff $diff - ($mos*2629743.83);
        
$showDiff .= empty($showDiff) ? '' ', ';
        
$showDiff .= "{$mos} month";
        
$showDiff .= $mos>'s' '';
    }
    unset(
$mos); // End Months

    // Start Weeks
    
$wks floor($diff/604800); // 604800 secs/wk
    
if ($wks 0)
    {
        
$diff $diff - ($wks*604800);
        
$showDiff .= empty($showDiff) ? '' ', ';
        
$showDiff .= "{$wks} week";
        
$showDiff .= $wks>'s' '';
    }
    unset(
$wks); // End Weeks

    // Start Days
    
$days floor($diff/86400); // 86400 secs/day
    
if ($days 0)
    {
        
$diff $diff - ($days*86400);
        
$showDiff .= empty($showDiff) ? '' ', ';
        
$showDiff .= "{$days} day";
        
$showDiff .= $days>'s' '';
    }
    unset(
$days); // End Days

    // Start Hours
    
$hrs floor($diff/3600); // 3600 secs/hr
    
if ($hrs 0) {
        
$diff $diff - ($hrs*3600);
        
$showDiff .= empty($showDiff) ? '' ', ';
        
$showDiff .= "{$hrs} hour";
        
$showDiff .= $hrs>'s' '';
    }
    unset(
$hrs); // End Hours

    // Start Minutes
    
$mins floor($diff/60); // 60 secs/min
    
if ($mins 0) {
        
$diff $diff - ($mins*60);
        
$showDiff .= empty($showDiff) ? '' ', ';
        
$showDiff .= "{$mins} minute";
        
$showDiff .= $mins>'s' '';
    }
    unset(
$mins); // End Minutes

    // Start Seconds
    
if ($diff 0)
    {
        
$showDiff .= empty($showDiff) ? '' ', ';
        
$showDiff .= "{$diff} second";
        
$showDiff .= $diff>'s' '';
    } 
// End Seconds

    
unset($diff); // Free unused memory

    // Return difference
    
if ($currTime<$origTime) { $showDiff "- {$showDiff}"; }
    return 
$showDiff;
}

// Examples:
echo CalcDiff(234892574923234892374923); // 2 days, 7 hours, 33 minutes, 20 seconds
echo CalcDiff(234892344923234892374923); // - 8 hours, 20 minutes
echo CalcDiff(23945349242392374923); // 3 weeks, 4 days, 1 second

?>

Last edited by Keith; 05-09-06 at 02:34 PM.
Reply With Quote
The Following User Says Thank You to Keith For This Useful Post:
n3wb!e (11-30-09)
  #4 (permalink)  
Old 05-12-06, 12:36 AM
n3wb!e's Avatar
n3wb!e n3wb!e is offline
Wannabe Coder
 
Join Date: Mar 2006
Posts: 216
Thanks: 2
Thanked 0 Times in 0 Posts
thanks dude.. thanks a ton.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Dates DAL PHP 5 04-22-06 07:00 AM
show dates in the future dripdrown PHP 7 06-02-05 01:52 AM
display all dates between two date dhzen JavaScript 0 07-21-04 11:53 PM
registration of prodid,name,and 2 dates pr. month ropey PHP 3 01-31-04 01:12 PM
How to find the difference between two dates and make multiple inserts based on it? wyseguy ASP 1 10-08-03 11:53 AM


All times are GMT -5. The time now is 11:22 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.