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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度