Page 1 of 1

Comparing two timestamps

Posted: Thu Sep 30, 2010 11:00 am
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

Re: Comparing two timestamps

Posted: Thu Sep 30, 2010 11:04 am
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

Re: Comparing two timestamps

Posted: Thu Sep 30, 2010 11:11 am
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.

Re: Comparing two timestamps

Posted: Thu Sep 30, 2010 11:25 am
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);

Re: Comparing two timestamps

Posted: Thu Sep 30, 2010 11:27 am
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

Re: Comparing two timestamps

Posted: Thu Sep 30, 2010 11:32 am
by twinedev
Expanding on pytrin's post, try:

Code: Select all

SELECT (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`ts_field`) AS SecondsOld FROM ......

Re: Comparing two timestamps

Posted: Thu Sep 30, 2010 11:42 am
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.

Re: Comparing two timestamps

Posted: Thu Sep 30, 2010 12:08 pm
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)

Re: Comparing two timestamps

Posted: Thu Sep 30, 2010 2:17 pm
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.