Current location: Hot Scripts Forums » Programming Languages » PHP » Difference between two "time();" stamps


Difference between two "time();" stamps

Reply
  #1 (permalink)  
Old 06-08-09, 06:31 AM
ZeroDay ZeroDay is offline
Newbie Coder
 
Join Date: Nov 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Question Difference between two "time();" stamps

Hello,

I'm wondering if anyone can help me?

In a database, I have an 'opened' and a 'closed' field. They both populate with the php time(); function.

I'm wondering if I can calculate and echo the difference between the two, in hours/minutes/seconds

Hope I've explained that properly.

Kind Regards,
ZD

Last edited by ZeroDay; 06-08-09 at 06:41 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-08-09, 07:20 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:

function date_calc_diff($timestamp_past$timestamp_future$years true$months true$days true$hours true$mins true$secs true)

{
    
$diff $timestamp_future $timestamp_past;
    
$calc_times = array();
    
$timeleft   = array();
    
    
// Prepare array, depending on the output we want to get.
    
if ($years)  $calc_times[] = array('Year',   'Years',   31104000);
    if (
$months$calc_times[] = array('Month',  'Months',  2592000);
    if (
$days)   $calc_times[] = array('Day',    'Days',    86400);
    if (
$hours)  $calc_times[] = array('Hour',   'Hours',   3600);
    if (
$mins)   $calc_times[] = array('Minute''Minutes'60);
    if (
$secs)   $calc_times[] = array('Second''Seconds'1);

    foreach (
$calc_times AS $timedata)
    {
        list(
$time_sing$time_plur$offset) = $timedata;
        
        if (
$diff >= $offset)
        {
            
$left floor($diff $offset);
            
$diff -= ($left $offset);
            
$timeleft[] = "{$left} " . ($left == $time_sing $time_plur);
        }
    }
    
    return 
$timeleft ? ($timestamp_future $timestamp_past null '-') . implode(', '$timeleft) : 0;


Usage example:
PHP Code:

$diff date_calc_diff($opened$closed);


echo 
$diff// 4 Days, 2 Hours, 29 Minutes, 14 Seconds 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-11-09, 05:57 AM
ZeroDay ZeroDay is offline
Newbie Coder
 
Join Date: Nov 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you very much. You are very kind!

This works perfectly.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-11-09, 06:12 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
No biggie!

I wrote this a while ago, so I just grabbed it from my archives.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
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
Date difference Miben PHP 1 03-30-09 04:18 AM
Difference in hours / minutes End User PHP 9 04-01-08 06:35 AM
Difference between the user control and Custom server controls bhar ASP.NET 0 07-22-06 06:42 AM
Difference between <%! and <% when declaring variables in .jsp flekso Everything Java 1 09-10-04 10:09 AM
Calculating time difference rocky PHP 2 07-17-04 03:48 PM


All times are GMT -5. The time now is 01:52 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.