Hi Guys,
I thought I should share this useful code snippet with you that will convert time in seconds to Days Hours Minutes Seconds format
Here is the function
function timeBeautifier($seconds_val){ $returnstring = " "; $secs = $seconds_val - ( ($dd*86400)+($hh*3600)+($mm * 60)); $returnstring .= ($dd)?(($dd == 1)? "1 day":$dd." days"):""; $returnstring .= ($dd && $hh && !$mm && !$secs)?" and ":" "; $returnstring .= ($hh)?( ($hh == 1)?"1 hour":$hh." hours"):""; $returnstring .= (($dd || $hh) && ($mm && !$secs))?" and ":" "; $returnstring .= ($mm)?( ($mm == 1)?"1 minute":"$mm minutes"):""; $returnstring .= (($dd || $hh || $mm) && $secs)?" and ":" "; $returnstring .= ($secs)?( ($secs == 1)?"1 second":$secs." seconds"):""; return ($returnstring); }
You can easily extend this function to include year/month/weeks information if you like.
Let me show you the way you can use this function
Code below
echo timeBeautifier(86400);
will result in output
1 Day
Code below
echo timeBeautifier(86410);
will result in output
1 Day and 10 seconds
echo timeBeautifier(5648);
will result in output
1 hour 34 minutes and 8 seconds
I hope that this code snippet will be useful to you.
If you end up modifying this code please share it using comments so that others can benefit.
Cheers
Advertisement





JC WordPress Coupon Revealer Plugin Pro License
Australian Street Names with City, State and Display Names only, Single Server License
Recent Comments