Jump to content

Difference In Time Between Timestamps


xProteuSx

Recommended Posts

I am having trouble finding the time between two timestamps. I have tried to research this, but I don't even understand this timestamp format. This is the format for the data I am given (I have no choice in this!):

 

2012-12-13T11:34:16.771Z

 

I don't even know what the T and Z stand for! .. and I can't seem to figure it out.

 

I have a second date to compare to this. For example:

 

2012-12-13T12:04:29.000Z

 

I have to figure out the difference in days, hours, minutes, and seconds.

 

There must be a php function that can return the difference, if only in seconds (I can handle it from there).

 

Any ideas? Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/271949-difference-in-time-between-timestamps/
Share on other sites

The "Z" stands for "Zulu", or otherwise known as GMT+0. The "T" is just syntactical sugar, to separate the date and time aspect of the timestamp. That format is otherwise known as ISO 8601.

 

Though, to easily calculate the differences between these two timestamps, I'd use the DateTime class.

Man, this timestamp is ridiculous (I am a noob).

 

Here is what I have tried to do:

 

$endtime = 2012-12-13T12:36:59.000Z;

$curtime = 2012-12-13T12:12:44.723Z;

 

Find the difference (the difference should be around 25min);

 

Here is what I have tried:

 

 

$endtime = strtotime($endtime);

$curtime = strtotime($curtime);

echo date_sub($auc_endtime,$ebaytime);

 

The issue is that I get the following error:

 

Warning: date_sub() expects parameter 1 to be DateTime, integer given

 

:(

 

Should I be using date_parse?

 

The issue for me is that I have to use something lightweight. I am comparing two timestamps; one is from eBay so I have to make an API call which delays my page loading time, the other is a timestamp from a database. The whole point is to speed up the process of calculating days, hours, minutes, and seconds between these timestamps.

Think I've got it figured out:

 

 

$auctime = date_parse($endtime);

$ebaytime = date_parse($curtime);

 

if (($auctime["month"] <= $ebaytime["month"]) && ($auctime["day"] <= $ebaytime["day"]) && ($auctime["hour"] <= $ebaytime["hour"]) && ($auctime["minute"] <= $ebaytime["minute"]) && ($auctime["second"] <= $ebaytime["second"]))

{//auction is over}

 

... seems to be getting it done.

You should be using the DateTime class (or date_create if you want to stick procedural).  Also you want to be using the DateTime::diff/date_diff function, not sub.

 

$d1 = new DateTime($endtime);
$d2 = new DateTime($curtime);

$diff = $d1->diff($d2);
var_dump($diff);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.