2

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.

Peon
  • 7,902
  • 7
  • 59
  • 100

3 Answers3

6
$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));
hakre
  • 193,403
  • 52
  • 435
  • 836
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
  • 1
    +1 Excellent, saves me digging around for all the formats (strtotime fell over in my test script). – Fluffeh Sep 21 '12 at 09:10
  • Thanx m8, but one more question. If I print it out like this `print_r( $interval->format('%a') );` I get day difference. Is there a simple way to do this to get `Total amount of seconds` difference? – Peon Sep 21 '12 at 09:11
  • 1
    @MihaiIorga: I extended the answer a little, you can save you some trouble with `sscanf` for fixed length string segments, make it's helpful. Also I'd say it's not wrong to have some function, however some danger is in not checking return values, e.g. 0 for false for `strtotime` (or the own function), as this will taint the diff operation. – hakre Sep 21 '12 at 09:59
  • I fixed the taint problem by throwing an exception. A bit rough maybe, but better to add if clauses all over the place. – hakre Sep 21 '12 at 10:02
0

You're better off using a standard date/time format to avoid doing all the work from scratch.

Take a look at the discussion here: PHP How to find the time elapsed since a date time?

Community
  • 1
  • 1
Snowcrash
  • 80,579
  • 89
  • 266
  • 376
-1

try with strcmp, of course the better way is to convert it to Datetime object before compare it

Marcus
  • 6,701
  • 4
  • 19
  • 28