Search code examples
javascriptphpfunctionvariablesreturn

Translating PHP strtotime function into Javascript


I have a PHP function which calculates the time difference (hr/min/sec) of two values:

$mf_start_time = '10:00:00';
$mf_end_time = '11:00:00';
$st_start_time = '10:00:00';
$st_end_time = '12:00:00';
$sn_start_time = '10:00:00';
$sn_end_time = '13:00:00';
$ph_start_time = '10:00:00';
$ph_end_time = '14:00:00';
                                                
function time_interval($start,$end) {
    $s = strtotime($start);
    $e = strtotime($end);
    if ($s < $e)
    {
        $a = $e - $s;
    }
    else
    {
        $e = strtotime('+1 day',$e);
        $a = $e - $s;
    }
                        
    $h = floor($a/3600);
    $m = floor(($a%3600)/60);
    $s = $a%60;
                        
    return trim(($h?$h.' hour ':'').($m?$m.' minute ':'').($s?$s.' second ':''));
}
            
$mf_duration  = time_interval($mf_start_time,$mf_end_time);
$st_duration  = time_interval($st_start_time,$st_end_time);
$sn_duration  = time_interval($sn_start_time,$sn_end_time);
$ph_duration  = time_interval($ph_start_time,$ph_end_time);
                    
echo $mf_duration;
echo $st_duration;
echo $sn_duration;
echo $ph_duration;

My output for this is:

1 hour
2 hour
3 hour
4 hour

Now I am trying to translate this into Javascript, my problem is that I need to get strtotime() to work the same.

Here is what I tried in Javascript:

            var mf_start_time = '10:00:00';
            var mf_end_time = '11:00:00';
            var st_start_time = '10:00:00';
            var st_end_time = '12:00:00';
            var sn_start_time = '10:00:00';
            var sn_end_time = '13:00:00';
            var ph_start_time = '10:00:00';
            var ph_end_time = '14:00:00';
                        
            function time_interval(start,end)
            {
                var s = strtotime(start);
                var e = strtotime(end);
                if (s < e)
                {
                    a = e - s;
                }
                else
                {
                    var e = strtotime('+1 day',e);
                    var a = e - s;
                }
                                    
                var h = floor(a/3600);
                var m = floor((a%3600)/60);
                var s = a%60;
                                    
                return trim((h?h+' hour ':'')+(m?m+' minute ':'')+(s?s+' second ':''));
            }
                        
            var mf_duration  = time_interval(mf_start_time,mf_end_time);
            var st_duration  = time_interval(st_start_time,st_end_time);
            var sn_duration  = time_interval(sn_start_time,sn_end_time);
            var ph_duration  = time_interval(ph_start_time,ph_end_time);

            console.log(mf_duration);
            console.log(st_duration);
            console.log(sn_duration);
            console.log(ph_duration);

This does not work because strtotime does not work for Javascript (error not defined). What can I use for that?


Solution

  • I did my best to replicate what your php does, however I don't know if this would be best practice in js, nor if it would be of the same use as you have in your php app. Hopefully this is a good starting point for you to see how js works 👍

    var mf_start_time = '10:00:00';
    var mf_end_time = '11:00:00';
    var st_start_time = '10:00:00';
    var st_end_time = '12:00:00';
    var sn_start_time = '10:00:00';
    var sn_end_time = '13:00:00';
    var ph_start_time = '10:00:00';
    var ph_end_time = '14:00:00';
    
    function time_interval(start, end) {
      var [sHour, sMinute, sSecond] = start.split(":");
      var [eHour, eMinute, eSecond] = end.split(":");
      var s = new Date();
      s.setHours(sHour, sMinute, sSecond);
      var e = new Date();
      e.setHours(eHour, eMinute, eSecond);
      var a;
      if (s.getTime() < e.getTime()) {
        a = e.getTime() - s.getTime();
      } else {
        e.setDate(e.getDate() + 1);
        a = e.getTime() - s.getTime();
      }
      
      a = a / 1000;
      
      var h = Math.floor(a/3600);
      var m = Math.floor((a%3600)/60);
      var s = a%60;
    
      return (
        (h ? h + ' hour ' : '') +
        (m ? m +' minute ':'') +
        (s ? s +' second ':'')
      ).trim();
    }
    
    var mf_duration = time_interval(mf_start_time, mf_end_time);
    var st_duration = time_interval(st_start_time, st_end_time);
    var sn_duration = time_interval(sn_start_time, sn_end_time);
    var ph_duration = time_interval(ph_start_time, ph_end_time);
    
    console.log(mf_duration);
    console.log(st_duration);
    console.log(sn_duration);
    console.log(ph_duration);