1

Use php and MySQL, I want to get the duration in php code between time obtained by get_date() function and time stored in table by NOW() sql function. How to do this? Help me please.

NobodyNada
  • 7,529
  • 6
  • 44
  • 51
AJ OP
  • 345
  • 2
  • 5
  • 16

2 Answers2

1

This might work:

$result = mysql_query("SELECT UNIX_TIMESTAMP() - " . time() . " AS diff");
$result = mysql_fetch_array($result);
$diff = $result['diff'];

$diff will contain the difference in seconds between the mysql server and the webserver

Edit: to get the HH:MM:SS difference

$result = mysql_query("SELECT TIMEDIFF(UNIX_TIMESTAMP(), " . time() . ") AS diff");
Flambino
  • 18,507
  • 2
  • 39
  • 58
  • @ Flambino: then create the function that return duration in year:month:day:hour:min:sec or does it has any function to do this? – AJ OP Aug 01 '11 at 02:20
1

Mysql dates will return as strings in the format 'YYYY-MM-DD HH:II:SS'. Use the strtotime($date_from_db) to convert it to PHP date. If you're getting the current date/time do not use the getdate() function go for now() instead. After that, use the same instruction from the link PHP How to find the time elapsed since a date time? .

Community
  • 1
  • 1
P. R. Ribeiro
  • 2,999
  • 1
  • 19
  • 20