-3

Actually I working on a php website where User ads some entries in to database. At the time of insertion he adds one date field which also stored into db. Now I want the the age (how much time elapsed after saving that entry into database) of that entry. For that I calling one function to which I am passing that date & then using strtotime() function I am getting that time in seconds. For getting the elapsed time I am subtracting that converted time from current time. And here I am getting the main problem. It gives me NEGATIVE value. And I am stuck here so long & I am unable to figure it out why its returning negative value. Please help me out in this. Here is my tried code.

function timeSpan($string) 
{ 
$timestamp = strtotime($string); 
$now = time(); 
$timeSpan = $now-$timestamp; 
...........
...........
}

Here I am passing the datetime as $string to the function which is saved in the db at the time of form submitting. I have saved the date with type "datetime". And now the $timeSpan variable is returning a negative value. Anybody have any idea why its returning negative value?

vickram
  • 67
  • 2
  • 12
  • possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Marcin Orlowski Aug 30 '13 at 13:06
  • Why do this in PHP – the database can already calculate that for you when you read the entries from it. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html – CBroe Aug 30 '13 at 13:17

2 Answers2

0

If your substraction is returning a negative value, its because the time() stored is somehow newer than the current time in your server.

time() is the amount in seconds passed since 1/1/1970.

If your saved timestamp is, for example, 111 and your server says that current time is 100, you will get a negative value.

JorgeeFG
  • 5,651
  • 12
  • 59
  • 92
  • Hey Jorge, I understand what you are talking. But can you please tell me how to overcome this issue? – vickram Aug 30 '13 at 13:44
  • Well, if you save the result to a variable, check if that variable is less than 0, then print what ever you want to print in that case, like `Undefined` or nothing. I would do a dump of all the dates stored to see which date they are, and know what is going on with the server. – JorgeeFG Aug 30 '13 at 13:46
  • Jorge I want the age of that entry. If the elapsed time is getting wrong then all further time will get wrong. I want to overcome this problem. I know if I give a condition for > 0 then it will not show that result. But I want the exact time. By your style I can get a positive result but will not get the correct result. Can you tell why its returning negative value? Please... – vickram Aug 31 '13 at 04:55
  • Hey guys I got the solution for this. Actually the function returns the server time. And that time is dependent upon where the server is located. Its returns that time. So I specified my timezone. And now its returning correct time. – vickram Aug 31 '13 at 07:58
0

Hey guys I got the solution for this. Actually the function returns the server time. And that time is dependent upon where the server is located. Its returns that time. So I specified my timezone. And now its returning correct time.

vickram
  • 67
  • 2
  • 12