note 40319 deleted from function.strtotime by aidan
| From: | aidan@php.net | Date: | Wed, 11 Aug 2004 17:54:41 +0000 |
| Subject: | note 40319 deleted from function.strtotime by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74483@lists.php.net to get a copy of this message | ||
Note Submitter: rudigier@noxx.at
----
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) );
?>