Comparing two timestamps

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Comparing two timestamps

Post by tonchily »

I got two variables. One is selected from mysql and is in the following format - "Y-m-d H:i:s" and the other is in the same format, but it generates the current time. I want to subtract the current time with the one I select from mysql and echo the result in seconds. What's the most simple way to do it?

Cheers
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Comparing two timestamps

Post by josh »

Code: Select all

$timestamp = new Zend_Date($timestamp);
$mysqlDate = new Zend_Date($mysqlDate);

var_dump($timestamp ->compare($mysqlDate));
I'd say Zend_Date. It will parse the time zones, taking the locale into account, Daylight savings (again taking the locale into account)

http://framework.zend.com/manual/en/zend.date.html
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: Comparing two timestamps

Post by tonchily »

josh wrote:

Code: Select all

$timestamp = new Zend_Date($timestamp);
$mysqlDate = new Zend_Date($mysqlDate);

var_dump($timestamp ->compare($mysqlDate));
I'd say Zend_Date. It will parse the time zones, taking the locale into account, Daylight savings (again taking the locale into account)

http://framework.zend.com/manual/en/zend.date.html
I can't get it to work. The website actually dies at where I've copied your code.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Comparing two timestamps

Post by John Cartwright »

Did you include the Zend_Date class?

Also next time you get an error, post the error. If you do not see the error, you probably need to enable error_reporting(E_ALL);
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Comparing two timestamps

Post by Eran »

select the timestamp from the database using the UNIX_TIMESTAMP() function. It will return the timestamp in seconds
http://dev.mysql.com/doc/refman/5.1/en/ ... -timestamp
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Comparing two timestamps

Post by twinedev »

Expanding on pytrin's post, try:

Code: Select all

SELECT (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`ts_field`) AS SecondsOld FROM ......
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: Comparing two timestamps

Post by tonchily »

pytrin wrote:select the timestamp from the database using the UNIX_TIMESTAMP() function. It will return the timestamp in seconds
http://dev.mysql.com/doc/refman/5.1/en/ ... -timestamp
Apparently I fail at that too. I've got this

<?php
$now = date("Y-m-d H:i:s");
echo UNIX_TIMESTAMP($now);
?>

and actually my code dies there.
Sorry, I'm new to this.

Thanks.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Comparing two timestamps

Post by Eran »

Did you visit the link I gave you? it's a mysql function, not PHP
in PHP you can use strtotime(), but bear in mind that you might lose timezone information (might not be relevant for you)
tonchily
Forum Commoner
Posts: 54
Joined: Thu Sep 02, 2010 10:44 am

Re: Comparing two timestamps

Post by tonchily »

pytrin wrote:Did you visit the link I gave you? it's a mysql function, not PHP
in PHP you can use strtotime(), but bear in mind that you might lose timezone information (might not be relevant for you)

I did. I still fail to get this to work. I don't know where to put the UNIX_TIMESTAMP(); command.
Post Reply