kilrizzy
kilrizzy

Reputation: 2943

PHP Comparing Timestamps

When trying to compare to php timestamps I seem to be getting an off value. Not sure what I am missing.

I would like my output to end up being something like

3days 5hours 10min since x

I am trying this out with something that should only be a difference of minutes, but end up getting numbers that are very off (31 days 19 hours 11min)

$timesince = time() - $firstdate;
$dsince = date("d",$timesince);
$hsince = date("G",$timesince);
$msince = date("i",$timesince);

I'm guessing there's a different way I need to do this?

Upvotes: 1

Views: 8397

Answers (3)

Paul DelRe
Paul DelRe

Reputation: 4039

You could try the function found here on php.net.

Upvotes: 0

Tadeck
Tadeck

Reputation: 137300

time() returns the current time in Unix timestamp format, as you probably already know. $firstdate has to be also in this format or it should be transformed using strtotime() PHP function.

The solution you are using is wrong - this will not work for longer differences etc. You have to make some additional calculations, transforming difference in seconds into difference informing about days, hours and minutes.

EDIT:

You can find sample and correct solution within Kohana 3 PHP famework (span() method) here: http://kohanaframework.org/3.1/guide/api/Kohana_Date#span

Upvotes: 1

user187291
user187291

Reputation: 53940

date() accepts an absolute timestamp value, not the difference between two values. You need to parse the difference manually (divide by 3600, then by 60 etc) There are some examples in the comments here http://php.net/manual/en/function.time.php

Upvotes: 4

Related Questions