2

Suppose the target time is 4.30 pm and the current time is 3.25 pm , how will i calculate the minutes remaining to reach the target time ? I need the result in minutes.

session_start();
$m=30;
//unset($_SESSION['starttime']);
if(!$_SESSION['starttime']){
   $_SESSION['starttime']=date('Y-m-d h:i:s');
}
$stime=strtotime($_SESSION['starttime']);
$ttime=strtotime((date('Y-m-d h:i:s',strtotime("+$m minutes"))));-->Here I want to calcuate the target time; the time is session + 30 minutes. How will i do that
echo round(abs($ttime-$stime)/60);

Krishnik

Nick
  • 1,799
  • 3
  • 23
  • 32
  • 1
    possible duplicate of [How to get time difference in minutes in PHP](http://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php) – miku Jan 06 '11 at 10:55
  • strtotime("+30 minutes"); returns current time + 30 minutes. How can i pass time other than the current time to strtotime(); ? – Nick Jan 06 '11 at 11:38
  • 1
    take a look at the documentation, its right there http://php.net/manual/en/function.strtotime.php the second (optional) parameter is the timestamp from which it calculates – Hannes Jan 06 '11 at 11:44
  • @Hannes Thanks i tried it earlier but did'nt work because i have passed wrong value, then i tried with a time stamp of the current time ... I think its working now. Thank you again – Nick Jan 06 '11 at 11:49

1 Answers1

3

A quick calculation of the difference between two times can be done like this:

  $start = strtotime("4:30");
  $stop = strtotime("6:30");
  $diff = ($stop - $start); //Diff in seconds
  echo $diff/3600; //Return 2 hours. Divide by something else to get in mins etc.

Edit* Might as well add the answer to your problem too:

$start = strtotime("3:25");
$stop = strtotime("4:30");
$diff = ($stop - $start);
echo $diff/60; //Echoes 65 min

Oh and one more edit:) If the times are diffent dates, like start is 23:45 one day and end is 0:30 the next you need to add a date too to the strtotime.

Sondre
  • 1,878
  • 18
  • 33