J. Cornelius

Converting Seconds in PHP

January 14, 2004 - 10:48:51 AM
All of the conversion functions that I found seemed really bloated. Here is a nice clean way to convert seconds to a more user friendly hour:minute:seconds format. This example takes the current Unix time and converts it to the desired format. Of course you can use this for just about any application that requires you to convert seconds to a more readable format. $seconds = time(); $time = str_pad(intval(intval($seconds/3600)),2,"0",STR_PAD_LEFT).":" . str_pad(intval(($seconds / 60) % 60),2,"0",STR_PAD_LEFT).":" . str_pad(intval($seconds % 60),2,"0",STR_PAD_LEFT) ; Now let's use this code for printing the time a particular script (or function) takes to run. Snazzy ! $start = time(); //--> some code here $finish = time(); $sec = ($finish - $start); $runtime = str_pad(intval(intval($sec) / 3600),2,"0",STR_PAD_LEFT).":" . str_pad(intval(($sec / 60) % 60),2,"0",STR_PAD_LEFT).":" . str_pad(intval($sec % 60),2,"0",STR_PAD_LEFT) ;

Tags

administration, development, php

About

J Cornelius is a software developer, Web developer, and Formula 1 fan in Atlanta GA. He has a strange affinity for odd numbers, european sports cars, thoughtful analogies, and is hopelessly addicted to chips & salsa. Read more