BLOG

Format Time Functions xx:xx in PHP and Javascript

Today I am going to show you format time from this format x ( seconds ) to xx:xx . And the reverse.

Javascript function from 90 to 1:30



            function formatTime(arg) {
                //formats the time
                var s = Math.round(arg);
                var m = 0;
                if (s > 0) {
                    while (s > 59) {
                        m++;
                        s -= 60;
                    }
                    return String((m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s);
                } else {
                    return "00:00";
                }
            }

Javascript function from 1:30 to 90



    function unformatTime(arg) {
        //formats the time


        if(arg.indexOf(':')>-1){
            var auxa = arg.split(':');


            return Number(auxa[0]) * 60 + Number(auxa[1]);

        }else{

            return arg;
        }

    }

PHP function from 1:30 to 90


function unformatTime($arg) {
        //formats the time


        if(strpos($arg, ':')!==false){

            $auxa = explode(':', $arg);
            return intval($auxa[0]) * 60 + intval($auxa[1]);

//            print_r($auxa);
        }else{

            return $arg;
        }

    }

Leave a comment

Your email address will not be published

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

26 − = 16