温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

javascript与php时/分/秒与秒数互转

发布时间:2020-06-13 21:40:57 来源:网络 阅读:697 作者:Lee_吉 栏目:web开发
  1. javascript:
    /*
    时间转换成秒
    */
    function time_to_second(time){
    var arr = time.split(':')
    var hour = arr[0]?arr[0]:0
    var minute = arr[1]?arr[1]:0
    var second = arr[2]?arr[2]:0
    var ret = hour * 3600 + minute * 60 + second
    return ret
    }
    /*
    秒转换成时间
    */
    function second_to_time(seconds){
    hour = Math.floor(seconds / 3600)
    minute = Math.floor((seconds - hour * 3600) / 60)
    second = seconds - hour * 3600 - minute * 60
    if(hour < 10){
        hour = "0" + hour
    }
    if(minute < 10){
        minute = "0" + minute
    }
    if(second < 10){
        second = "0" + second
    }
    ret = hour + ':' + minute + ':' + second
    return ret
    }
    // var time = "10:00:00"
    // var ret = time_to_second(time)
    // console.log(ret)
    var seconds = 36000
    var ret = second_to_time(seconds)
    console.log(ret)
  2. php:
    <?php
    /*
    时间转换成秒
    */
    function time_to_second($time){
    if(!$time){
        $ret = 0;
    }else{
        $parsed = date_parse($time);
        $ret = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
    }
    return $ret;
    }
    /*
    秒转换成时间
    */
    function second_to_time($seconds){
    $hour = floor($seconds / 3600);
    $minute = floor(($seconds - $hour * 3600) / 60);
    $second = $seconds - $hour * 3600 - $minute * 60;
    if($hour < 10){
        $hour = "0{$hour}";
    }
    if($minute < 10){
        $minute = "0{$minute}";
    }
    if($second < 10){
        $second = "0{$second}";
    }
    $ret = "{$hour}:{$minute}:{$second}";
    return $ret;
    }
    // $time = "01:00:00";
    // $ret = time_to_second($time);
    // echo $ret;
    $seconds = 3600;
    $ret = second_to_time($seconds);
    echo $ret;
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI