austin
austin

Reputation: 33

How do I subtract time in PHP?

So, I have the following being returned by an API:

<startTime>2011-04-12T01:28:40.000Z</startTime>

It's in UTC/Zulu format. How would I go about getting the time elapsed (in seconds) since that timestamp in PHP?

Thanks for any pointers!

Upvotes: 3

Views: 7090

Answers (4)

alex
alex

Reputation: 490143

$now = new DateTime;

$zulu = new DateTime('2011-04-12T01:28:40.000Z');

$difference = $now->diff($zulu);

diff() is supported in >= PHP 5.3.

Otherwise, you can use time() and strtotime().

$difference = time() - strtotime('2011-04-12T01:28:40.000Z');

Upvotes: 8

Steven Mercatante
Steven Mercatante

Reputation: 25295

Check out the strtotime (http://us.php.net/manual/en/function.strtotime.php) and time functions (http://us.php.net/manual/en/function.time.php). Basically you would convert your timestamp to an integer, then subtract it from the current time.

Upvotes: 1

Traveling_Monk
Traveling_Monk

Reputation: 788

$elapsed = time () - strtotime ( $startTime);

Upvotes: 0

Emmanuel
Emmanuel

Reputation: 5395

Or

$now = time();

$zulu = strtotime('2011-04-12T01:28:40.000Z');

$difference = $now - $zulul

Upvotes: 1

Related Questions