Comparing two timestamps
Moderator: General Moderators
Comparing two timestamps
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
Cheers
Re: Comparing two timestamps
Code: Select all
$timestamp = new Zend_Date($timestamp);
$mysqlDate = new Zend_Date($mysqlDate);
var_dump($timestamp ->compare($mysqlDate));
http://framework.zend.com/manual/en/zend.date.html
Re: Comparing two timestamps
I can't get it to work. The website actually dies at where I've copied your code.josh wrote:I'd say Zend_Date. It will parse the time zones, taking the locale into account, Daylight savings (again taking the locale into account)Code: Select all
$timestamp = new Zend_Date($timestamp); $mysqlDate = new Zend_Date($mysqlDate); var_dump($timestamp ->compare($mysqlDate));
http://framework.zend.com/manual/en/zend.date.html
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Comparing two timestamps
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);
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
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
http://dev.mysql.com/doc/refman/5.1/en/ ... -timestamp
Re: Comparing two timestamps
Expanding on pytrin's post, try:
Code: Select all
SELECT (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`ts_field`) AS SecondsOld FROM ......Re: Comparing two timestamps
Apparently I fail at that too. I've got thispytrin 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
<?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
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)
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
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.