Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8567125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:48:41+00:00 2026-06-11T17:48:41+00:00

I have two timestamps that are formated like this: substr( date( ‘YmdHisu’ ), 0,

  • 0

I have two timestamps that are formated like this: substr( date( 'YmdHisu' ), 0, 17 )

20120921105240000
20120921115626000

Now, how do I compare them?

I tried to do a simple approach like:

$diff = abs( 20120921105240000 - 20120921115626000 );

But this doesn’t give me the needed result, since the time messes it up. All I want to do, is find out, how many minutes( or seconds ) have passed between them.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:48:42+00:00Added an answer on June 11, 2026 at 5:48 pm
    $date1 = DateTime::createFromFormat('Ymdhisu', '20120921105240000');
    $date2 = DateTime::createFromFormat('Ymdhisu', '20120921115626000');
    $interval = $date1->diff($date2);
    print_r($interval);
    
    /*DateInterval Object
    (
        [y] => 0
        [m] => 0
        [d] => 0
        [h] => 1
        [i] => 3
        [s] => 46
        [invert] => 0
        [days] => 0
    )*/
    
    $seconds = $date2->getTimestamp()-$date1->getTimestamp();
    echo $seconds;
    

    Example

    See extra info on the object and method.

    For PHP < 5.3:

    Method sscanf + mktime:

    $date1 = '20120921105240000';
    $date2 = '20120921115626000';
    
    function parse_mydate_string($string) {
        if ($a = sscanf($string, '%4s%2s%2s%2s%2s%2s') {
            if (FALSE !== $r = mktime($a[3], $a[4], $a[5], $a[1], $a[2], $a[0])) {
                return $r;
            }
        }
        throw new InvalidArgumentException('Not the expect date string.');
    }
    
    $diff    = parse_mydate_string($date1) - parse_mydate_string($date2);    
    $absdiff = abs($diff);
    

    or sscanf + vsprintf + strtotime:

    $date1 = '20120921105240000';
    $date2 = '20120921115626000';
    
    $date1 = vsprintf('%s-%s-%s %s:%s:%s', sscanf($date1, '%4s%2s%2s%2s%2s%2s'));
    $date2 = vsprintf('%s-%s-%s %s:%s:%s', sscanf($date2, '%4s%2s%2s%2s%2s%2s'));
    
    $diff = abs(strtotime($date2) - strtotime($date1));
    

    or multiple substr string operations + strtotime:

    $date1 = '20120921105240000';
    $date2 = '20120921115626000';
    
    $date1 = substr($date1, 0, 4).'-'.substr($date1, 4, 2).'-'.substr($date1, 6, 2).' '.substr($date1, 8, 2).':'.substr($date1, 10, 2).':'.substr($date1, 12, 2);
    $date2 = substr($date2, 0, 4).'-'.substr($date2, 4, 2).'-'.substr($date2, 6, 2).' '.substr($date2, 8, 2).':'.substr($date2, 10, 2).':'.substr($date2, 12, 2);
    
    $diff = abs(strtotime($date2) - strtotime($date1));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does Ruby stdlib have any objects that represent difference between two timestamps? Subtracting two
I have two unix timestamps in my database that I am subtracting to get
I have a MySQL query that uses two different timestamps that can be perceived
I have a log file that consists of many timestamps in this fashion: [2010/02/21
I have two byte[] (like this { 0, 0, 0, 0, 52, 246, 141,
Hey I have this little batch file that is supposed to compare the timestamps
I have two data frames with two columns each. The first column is timestamps
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two fields with timestamps type, time_in and time_out. For example: time_out =
I have a string that has a file path: $workingFile = '/var/tmp/A/B/filename.log.timestamps.etc'; I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.