开发者

How do I get the difference between a mysql timestamp and the php current time?

Th开发者_JS百科e question i guess is kind of 2 questions:

  1. How do i even GET the current time in php and in a format that is easily compared to and
  2. Once i have that, i have a mysql timestamp in this format, 2011-06-30 04:33:00 , that I need to be compared to the php current time.

thank you so so much in advance, Binny


I'm assuming you're storing it as a DATETIME column. As such, in MySQL

SELECT
    UNIX_TIMESTAMP(`date_column`) AS `timestamp`,
    ...
FROM
    ...

Then, in PHP:

$time_diff_in_seconds = time() - $query_result['timestamp']

However, I'd just let the database do it:

SELECT
    TIME_TO_SEC(TIMEDIFF(CURRENT_TIMESTAMP, `date_column`)) AS time_diff,
    ...
FROM
    ...


strtotime($query_result['timestamp'])

This will convert your MySQL timestamp value to the correct seconds since Jan 1, 1970 value. Then it's just a matter of subtracting the two to get the difference.


One thing you should check: are the two times coming from the same machine? If they are coming from different machines, you should worry about time zones and synchronization.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜