TimeAgo function returning negative seconds

时间:2016-02-12 20:56:15

标签: php timeago

I have a function that returns how long ago the time was posted, but there seem be to a slight problem with it. The first time, $timeAgo1 works just fine, but the second time, $timeAgo2 seems to return negative seconds. How is that happening?

module-info name

2 个答案:

答案 0 :(得分:1)

Set the timezone in the beginning of the script to the same date as original dates you are checking against, example:

function db_connect() { static $connection; if(!isset($connection)) { $connection = mysqli_connect('localhost','user','password','Verb_Bank'); } if($connection === false) { return mysqli_connect_error(); } return $connection; } function db_query($query) { $connection = db_connect(); $result = mysqli_query($connection,$query); return $result; } function db_quote($value) { $connection = db_connect(); return "'" . mysqli_real_escape_string($connection,$value) . "'"; } $m= db_query("SELECT `conjugation` from normal_verbs where `root` in (" . $y . ")"); if($m === false) { // Handle failure - log the error, notify administrator, etc. } else { // Fetch all the rows in an array $rows = array(); while ($row = mysqli_fetch_assoc($m)) { $rows[] = $row; } } print_r ($rows);

http://php.net/manual/en/function.date-default-timezone-set.php

答案 1 :(得分:0)

It is probably something with your server because I used 2 different servers and the results are

NSObjects?

It is possible that your server is located somewhere else than you are, so the 166733 20094 2 days ago 6 hours ago function shows a different timestamp from what you might expect.

The timestamp of the second timeAgo is 1455290705 so your server is probably located somewhere where it is/was more than 1455290705.