码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-10-06 00:33:59      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:return   cond   ons   func   转换   console   time_t   sed   spl   

  • 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)
  • 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;
  • javascript与php时/分/秒与秒数互转

    标签:return   cond   ons   func   转换   console   time_t   sed   spl   

    原文地址:http://blog.51cto.com/12173069/2294025

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!