Call: 09 280 3569
(021 0272 9174)
 

Php Format Date Time Strings

Often we need to format a date time string into a more human readable format. Below are a few code snippets we often use.

Format Minutes to Day, Hour, Minutes

$minutes = 4400; # Integer
$d = floor($minutes / 1440);
$h = floor(($minutes - $d * 1440) / 60);
$m = $minutes - ($d * 1440) - ($h * 60);
$str = '';
if($d>0){$str .= $d . 'd ';}
if($h>0){$str .= $h . 'h ';}
if($m>0){$str .= $m . 'm ';}
echo trim($str);

Format Seconds to Hour, Minutes, Seconds

$seconds = 3699; # Integer
$h = floor($seconds / 3600);
$m = floor(($seconds - $h * 3600) / 60);
$s = floor($seconds - ($h * 3600) - ($m * 60));
$str = '';
if($h>0){$str .= $h . 'h ';}
if($m>0){$str .= $m . 'm ';}
if($s>0){$str .= $s . 's ';}
echo trim($str);
Facebook
Twitter
Grab this page

Copy this page to your mobile device using the below QR code.

QR Code for this Url
Web Vine © 2009 - 2013