adding two time values of similar formats using php

前端 未结 7 769
轮回少年
轮回少年 2020-12-03 11:41

i have two time values as give below

$time  = 06:58:00;
$time2 = 00:40:00;

I am doing this for calculating the appointments and available

相关标签:
7条回答
  • 2020-12-03 12:23

    Easiest way to add two times using php is :

    1) Convert time from H:i:s (e.g. 08:15:40) format to seconds.
    2) do the same for second time value ref:step 1
    3) add converted values and store it php variable
    4) Now convert total (which is in seconds) to H:i:s
    and it works for me.

    PHP Script:

    $str_time ="08:04:40";
    
    $str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
    
    sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
    
    $hrs_old_seconds = $hours * 3600 + $minutes * 60 + $seconds;
    
    $str_time ="02:10:22";
    
    $str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);
    
    sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
    
    $hrs_toadd_seconds = $hours * 3600 + $minutes * 60 + $seconds;
    
    $hrs_old_int1 = $hrs_old_seconds + $hrs_toadd_seconds;
    
    echo $Total=gmdate("H:i:s", $hrs_old_int1);
    

    Result= :10:15:02

    0 讨论(0)
提交回复
热议问题