duan47676379 2016-11-27 19:41
浏览 74
已采纳

如何使用PHP减去两个时间值?

I have done almost everything I found on the internet. I have been trying for the past two days but couldn't get the desire answer. I just want to subtract two times. example 22:00:00 - 00:30:00 = 21:30:00

$hourToEatLastMeal = strtotime('00:30:00');
$hourToEatWorkout = strtotime('01:30:00');
$hourBetweenMeal = strtotime('07:30:00');
$lastMealRequested = strtotime($row['lastMeal']); //22:00:00 //value getting from query.

$startEating = $lastMealRequested - $hourToEatLastMeal ;//21:30:00 (want this answer)
$timeToEat = $startEating- $hourBetweenMeal; //14:00:00 (want this answer)
$whenToWorkout = $timeToEat - $hourToEatWorkout; //12:30:00 (want this answer)

$whenToWorkout = date("H:i:s",($whenToWorkout));//12:30:00 (want this answer)
$timeToEat = date("H:i:s",($timeToEat));//14:00:00 (want this answer)
$startEating = date("H:i:s",($startEating));//21:30:00  (want this answer)
  • 写回答

3条回答 默认 最新

  • dousuitang5239 2016-11-27 20:13
    关注

    00:30:00 is half past midnight, not 30 minutes. You can subtract 30 minutes with the strtotime function by using -30 minutes.

    echo date('H:i:s', strtotime('22:00:00 -30 minutes'));
    

    Other intervals can be done similarly:

    echo date('H:i:s', strtotime('22:00:00 -1 hour -30 minutes'));
    

    In your recent example you'd want:

    echo date('H:i:s', strtotime($lastMealRequested . ' -30 minutes')); 
    

    because you want the strtotime to do the math, not PHP (or not PHP directly).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计