PHP:如何以分钟计算剩余时间?

时间:2021-12-21 02:58:47

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.

假设目标时间是下午4点30分,当前时间是下午3点25分,我将如何计算达到目标时间的剩余时间?我需要几分钟的结果。

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

1 个解决方案

#1


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.

哦,还有一个编辑:)如果时间是不同的日期,比如开始是一天23:45而结束是0:30,那么你需要为strtotime添加一个日期。

#1


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.

哦,还有一个编辑:)如果时间是不同的日期,比如开始是一天23:45而结束是0:30,那么你需要为strtotime添加一个日期。