how to calculate two different time .. |
[eluser]Sawariya[/eluser]
Hi friends.. How to calculate two different time in PHP... Any idea.. I Have to time.. one session stating time and another one is current system time... how i wll get difference of these time... Thanks in Advance Sawariya
[eluser]gtech[/eluser]
current_system_time - session_starting_time = time_difference or is that not your question? Code: <?php
[eluser]Sawariya[/eluser]
Tnaks for your reply ,,,,,, I want to get a time difference. To get this I have to subtract the time which i stored in session from current time e.g. 08:40:15 from 16:48:30. So the starttime is 08:08:15 .current time is 08:40:15. Is there any function or component is available in php to do so, or I do it by myself?
[eluser]xwero[/eluser]
If the values are in the database and you use mysql 4.1.1+ you can use TIMEDIFF, in php there is no function for this so you have to write it yourself.
[eluser]Pascal Kriete[/eluser]
/sighs - time in php is stored as the number of seconds since the Unix Epoch. Php.net is a great resource. So since this is a time string you can use the strtotime function to get the unix times and then subtract them. @xwero: $unix = strtotime($mysql_datetime_field. " GMT");
[eluser]Sawariya[/eluser]
thank you for your reply if you don't mind can you explain ... i tried in different ways.. couldn't solve it
[eluser]xwero[/eluser]
@inparo : to process it in php you need to get them in timestamp form. Why should you want to do that if mysql provides a function to do it for you. You also assume it's a datetime field but what if it's a time field? strtotime("1970-01-01 ".$mysql_datetime_field. " GMT") feels hackish.
[eluser]Pascal Kriete[/eluser]
Xwero: of course you wouldn't do that, but Sawariya just said he had the time, there was no mention of sql. I was playing devil's advocate, since you don't necessarily 'have to write it yourself'. That said, use whatever fits the situation - for stuff like registration dates mysql time is fine, but if I need to do lots of manipulations I prefer unix timestamps (and store them as such in my db).
[eluser]xwero[/eluser]
I agree don't need to write a function for it but you have to, at least, write the subtraction that is how i interpreted the question. I fully agree you have to use the solution that is best for the case at hand. As for db preferences i go with mysql timestamps. |
Welcome Guest, Not a member yet? Register Sign In |