Difference between 2 unix timestamps
Results 1 to 4 of 4

Thread: Difference between 2 unix timestamps

  1. #1
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    Difference between 2 unix timestamps

    Does anyone know of either an easy way or a code snippet that can display the difference between 2 unix timestamps, a start date and a end date, and display the result in the number of days, hours and minutes.

    The start date is a timestamp stored in a variable called $startdate and the end date is in a variable called $enddate.


  2. #2
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: Difference between 2 unix timestamps

    This should work:

    ## example start/end dates
    $startdate = ( time() - 3587 );
    $enddate = time();

    ## difference between the two in seconds
    $time_period = ( $enddate - $startdate );

    $days = 0;
    $hours = 0;
    $minutes = 0;
    $seconds = 0;

    $time_increments = array( 'Days' => 86400,
    'Hours' => 3600,
    'Minutes' => 60,
    'Seconds' => 1 );

    ## will hold our values for ( day, minute, hour, seconds )
    $time_span = array();

    ## cycle through time_increments
    while( list( $key, $value ) = each( $time_increments )) {
    $this_value = (int) ( $time_period / $value );
    $time_period = ( $time_period % $value );

    # save value
    $time_span[$key] = $this_value;
    }


    ## show results
    while( list( $key, $value ) = each( $time_span )) {
    print "$key $value<br>\n";
    }


    HTH
    Stew

  3. #3
    Senior Member
    Join Date
    Jun 2000
    Posts
    65,357

    RE: Difference between 2 unix timestamps

    Thanks Steve

    That works perfectly. And it's about 10 times simpler than a version I used to use in a CGI script.

    Neil Baker

  4. #4

    RE: Difference between 2 unix timestamps

    Hi,

    I want to find difference in seconds for the two given date&time. who to do it. will u help me.

    Kamal

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •