CodeIgniter Forums
how to calculate two different time .. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: how to calculate two different time .. (/showthread.php?tid=8904)

Pages: 1 2


how to calculate two different time .. - El Forum - 06-04-2008

[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


how to calculate two different time .. - El Forum - 06-05-2008

[eluser]gtech[/eluser]
current_system_time - session_starting_time = time_difference


or is that not your question?

Code:
<?php
class Temp extends Controller {

  function Temp()
  {
    parent::Controller();
  }
  function index()
  {
    $created = microtime(true);  
    sleep(5);
    $finish = microtime(true);

    $diff = $finish - $created;
    echo $diff." time to load (in micro seconds)<br>";
  }
}
?&gt;



how to calculate two different time .. - El Forum - 07-14-2008

[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?


how to calculate two different time .. - El Forum - 07-14-2008

[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.


how to calculate two different time .. - El Forum - 07-14-2008

[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");


how to calculate two different time .. - El Forum - 07-14-2008

[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


how to calculate two different time .. - El Forum - 07-14-2008

[eluser]Sawariya[/eluser]
I am using mysql 5.0


how to calculate two different time .. - El Forum - 07-14-2008

[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.


how to calculate two different time .. - El Forum - 07-14-2008

[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).


how to calculate two different time .. - El Forum - 07-14-2008

[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.