note 40319 added to function.strtotime

From: Date: Sat, 28 Feb 2004 16:46:53 +0000
Subject: note 40319 added to function.strtotime
Groups: php.notes 
Request: Send a blank email to php-notes+get-65940@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 $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) ); ---- Manual Page -- http://www.php.net/manual/en/function.strtotime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40319 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40319&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40319&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#65940) next »