Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple time difference question

Status
Not open for further replies.

krappleby

Programmer
Jul 25, 2006
25
GB
hi all.,

i am trying to get the number of seconds between the current date/time and a datetime stamp in a mysql database..

i am pulling the data from the database, and have tried all sorts, the lastest is

echo mktime(date('Y-m-d H:i:s')) - mktime($resultrow['DATECHECK']);

however all i am getting is 0, i need to work out how many seconds difference there is.. can anyone help

cheers
 
The method of getting the difference will depend on the format of your mysql field.

If its in linux timestamp format its a simple subtraction, but if its in string format or something else, then a conversion needs to be done beforehand.

You might want to try something like:

$seconds=date("U")-(date("U",$resultrow['DATECHECK']));

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
ok.. using your code above, i get teh following

1180339247 1180339247

no matter how i try to i cannot get the date to subtract.. its just printing both stamps..

 
By what you posted, i see both of your dates are the same.

If i do this i get 60 seconds as the difference:

Code:
$date1=strtotime("12 February 2007 10:23:10");
$date2=strtotime("12 February 2007 10:24:10");
$seconds=date("U",$date1)-date("U",$date2);

echo "$date2 minus $date1 equals $seconds";

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
1180306800 minus 1180343179 equals 36379

is what i am getting.. i have made a couple of changes as follows

$date1=strtotime(DATE());
$date2=strtotime($resultrow['DATECHECK']);
$seconds=date("U",$date2)-date("U",$date1);
echo "$date1 minus $date2 equals $seconds<br>";

but its giving wat too many seconds. it sdhould only read about 10... so there is something wrong somewhere. the datetime in the database, is a datetime field. and appears as follows

2007-05-28 10:06:19

this is the exact record for the one above.. so in actual fact the answer should result in 360



cheers
 
i reckon this is, at least in part, a timezone issue.

what is the timezone of the mysql server and what is the timezone of the server on which php resides?

try, for the first test, retrieving the value of your timestamp field as follows
Code:
select utc_timestamp(timestampfield) from table
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top