note 40319 modified in function.strtotime by aidan

From: Date: Sat, 05 Jun 2004 04:19:55 +0000
Subject: note 40319 modified in function.strtotime by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-70652@lists.php.net to get a copy of this message
I've created a litte nice function to convert float times (eg. 4.5 h) in a real format (4h 30 min). Here's the code <?php $hours = "Hour(s)"; $minutes = "Minutes(s)"; function tohourmin($float) { global $hours, $minutes; $x = split("\.", (float) round($float,2)); $s = count(func_get_args())==2?true:false; // simple $hs = $s?'h':" ".$hours; $mins = $s?'\'':" ".$minutes; if(isset($x[1])) // reconverting { if ( ($x[1]>=1&&$x[1]<=9)&& strlen((string)$x[1])==1) $x[1]*=10; $x[1] = round($x[1]*6/10); } if(count(func_get_args())==3) return $x; // return array if (count($x)==2) if ($x[0]) return $x[0].$hs." ".$x[1].$mins; else return $x[1].$mins; else if (count($x)==1) return (int)$float.$hs; else return false; } $res1 = tohourmin(2.5); // 2 Hour(s) 30 Minute(s) $res2 = tohourmin(2.5,true); // 2 h 30 ' $res3 = tohourmin(2,5,true,true); // array(2, 30) ?> To generate a float value of $_hours and $_mins i suggest <? // converting mins to floatformat $_mins *= 10/6; if($_mins<10) $_mins/=10; $hours_float = floatval($_hours.".".str_replace(".",'',$_mins) ); ?> --was-- I've created a litte nice function to convert float times (eg. 4.5 h) in a real format (4h 30 min). Here's the code $hours = "Hour(s)"; $minutes = "Minutes(s)"; function tohourmin($float) { global $hours, $minutes; $x = split("\.", (float) round($float,2)); $s = count(func_get_args())==2?true:false; // simple $hs = $s?'h':" ".$hours; $mins = $s?'\'':" ".$minutes; if(isset($x[1])) // reconverting { if ( ($x[1]==1||$x[1]==2||$x[1]==3||$x[1]==4||$x[1]==5) && strlen((string)$x[1])==1) $x[1]*=10; $x[1] = round($x[1]*6/10); } if(count(func_get_args())==3) return $x; // return array if (count($x)==2) if ($x[0]) return $x[0].$hs." ".$x[1].$mins; else return $x[1].$mins; else if (count($x)==1) return (int)$float.$hs; else return false; } $res1 = tohourmin(2.5); // 2 Hour(s) 30 Minute(s) $res2 = tohourmin(2.5,true); // 2 h 30 ' $res3 = tohourmin(2,5,true,true); // array(2, 30) To generate a float value of $_hours and $_mins i suggest // converting mins to floatformat $_mins *= 10/6; if($_mins<10) $_mins/=10; $hours_float = floatval($_hours.".".str_replace(".",'',$_mins) ); http://www.php.net/manual/en/function.strtotime.php

« previous php.notes (#70652) next »