1<?php
2
3/**
4 *    pChart - a PHP class to build charts!
5 *
6 *    This program is free software: you can redistribute it and/or modify
7 *    it under the terms of the GNU General Public License as published by
8 *    the Free Software Foundation, either version 1,2,3 of the License, or
9 *    (at your option) any later version.
10 *
11 *    This program is distributed in the hope that it will be useful,
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *    GNU General Public License for more details.
15 *
16 *    You should have received a copy of the GNU General Public License
17 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/**
21 * Provides static helper methods for converting values from one
22 * format to another for display.
23 *
24 * These methods don't have state, they're only in a class for
25 * purposes of namespacing.
26 */
27class ConversionHelpers {
28    /**
29     * Convert seconds to a time format string
30     */
31    static public function ToTime($Value) {
32        $Hour   = floor($Value / 3600);
33        $Minute = floor(($Value - $Hour * 3600) / 60);
34        $Second = floor($Value - $Hour * 3600 - $Minute * 60);
35
36        if(strlen($Hour) == 1) {
37            $Hour = "0".$Hour;
38        }
39        if(strlen($Minute) == 1) {
40            $Minute = "0".$Minute;
41        }
42        if(strlen($Second) == 1) {
43            $Second = "0".$Second;
44        }
45
46        return ($Hour.":".$Minute.":".$Second);
47    }
48
49    /**
50     * Convert to metric system
51     */
52    static public function ToMetric($Value) {
53        $Go = floor($Value / 1000000000);
54        $Mo = floor(($Value - $Go * 1000000000) / 1000000);
55        $Ko = floor(($Value - $Go * 1000000000 - $Mo * 1000000) / 1000);
56        $o  = floor($Value - $Go * 1000000000 - $Mo * 1000000 - $Ko * 1000);
57
58        if($Go != 0) {
59            return ($Go.".".$Mo."g");
60        }
61        if($Mo != 0) {
62            return ($Mo.".".$Ko."m");
63        }
64        if($Ko != 0) {
65            return ($Ko.".".$o)."k";
66        }
67        return ($o);
68    }
69
70    /**
71     * Convert to curency
72     *
73     * @param float $Value
74     * @return string
75     */
76    static public function ToCurrency($Value) {
77        $Go = floor($Value / 1000000000);
78        $Mo = floor(($Value - $Go * 1000000000) / 1000000);
79        $Ko = floor(($Value - $Go * 1000000000 - $Mo * 1000000) / 1000);
80        $o  = floor($Value - $Go * 1000000000 - $Mo * 1000000 - $Ko * 1000);
81
82        if(strlen($o) == 1) {
83            $o = "00".$o;
84        }
85        if(strlen($o) == 2) {
86            $o = "0".$o;
87        }
88
89        $ResultString = $o;
90        if($Ko != 0) {
91            $ResultString = $Ko.".".$ResultString;
92        }
93        if($Mo != 0) {
94            $ResultString = $Mo.".".$ResultString;
95        }
96        if($Go != 0) {
97            $ResultString = $Go.".".$ResultString;
98        }
99
100        return ($ResultString);
101    }
102}