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>

8 + 1 =

{"type":"main_options","images_arr":"'#ffffff'","enable_ajax":"off","soundcloud_apikey":"","bg_isparallax":"off","bg_slideshow_time":"0","bg_transition":"slidedown","site_url":"http:\/\/digitalzoomstudio.net","theme_url":"http:\/\/digitalzoomstudio.net\/wp-content\/themes\/qucreative\/","blur_ammount":"26","width_column":"50","width_section_bg":"","width_gap":"30","border_width":"0","border_color":"#ffffff","translate_leave_a_comment":"Leave a comment","translate_leave_a_comment_to":"Leave a comment to","is_customize_preview":"off","width_blur_margin":"30","gallery_w_thumbs_autoplay_videos":"off","enable_native_scrollbar":"on","content_enviroment_opacity":"30","menu_enviroment_opacity":"70"}
{"type":"darkfull"}