0

I have statistics that require a 'Last Updated' section on a dynamic image, and is expressed in hours. This is done with the following:

$lastDate = $StatData->date;
$now = date("Y-m-d G:i:s");
$hours = (strtotime($now) - strtotime($lastDate)) / 3600;

Displaying $hours on the image with an imagettftext works just fine. It's just that it's up to 16 characters in length. So to fix that, I attempted the following:

if($hours < 10) {
    substr($hours,0,4);
} else {
    substr($hours,0,5);
}

The intention was to display no more than two decimals. The result was no change. As if the code wasn't even there.

So my question is one of two: 1: How do i get this to work? 2: I know there is a way to limit the decimal places, without using substr, but what is it? Might be a better solution.

4

2 回答 2

0

使用round()

$hours = round((strtotime($now) - strtotime($lastDate)) / 3600, 2); // 2 decimals
于 2013-04-28T20:03:06.583 回答
0

您也可以使用number_format

$hours = number_format($hours, 2);
于 2013-04-28T20:05:48.020 回答