FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
timer.php
1 <?php
2 
3 /***********************************************************
4  Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
42 class timer
43 {
44  public $startTime;
45 
46  public function __construct()
47  {
48  $this->startTime = time();
49  }
50  public function getStartTime()
51  {
52  return ($this->startTime);
53  }
66  public static function TimeAgo($timestamp)
67  {
68  // Store the current time
69  $current_time = time();
70 
71  // Determine the difference, between the time now and the timestamp
72  $difference = $current_time - $timestamp;
73 
74  // Set the periods of time
75  $periods = array (
76  "second",
77  "minute",
78  "hour",
79  "day",
80  "week",
81  "month",
82  "year",
83  "decade"
84  );
85 
86  // Set the number of seconds per period
87  $lengths = array (
88  1,
89  60,
90  3600,
91  86400,
92  604800,
93  2630880,
94  31570560,
95  315705600
96  );
97 
98  // Determine which period we should use, based on the number of seconds lapsed.
99  // If the difference divided by the seconds is more than 1, we use that. Eg 1 year / 1 decade = 0.1, so we move on
100  // Go from decades backwards to seconds
101  for ($val = sizeof($lengths) - 1;($val >= 0) && (($number = $difference / $lengths[$val]) <= 1); $val--);
102 
103  // Ensure the script has found a match
104  if ($val < 0)
105  $val = 0;
106 
107  // Determine the minor value, to recurse through
108  $new_time = $current_time - ($difference % $lengths[$val]);
109 
110  // Set the current value to be floored
111  $number = floor($number);
112 
113  // If required create a plural
114  if ($number != 1)
115  $periods[$val] .= "s";
116 
117  // Return text
118  $text = sprintf("%d %s ", $number, $periods[$val]);
119 
120  // Ensure there is still something to recurse through, and we have not found 1 minute and 0 seconds.
121  if (($val >= 1) && (($current_time - $new_time) > 0))
122  {
123  $text .= self :: TimeAgo($new_time);
124  }
125  return $text;
126  }
127 }
128 ?>
static TimeAgo($timestamp)
Definition: timer.php:66
Definition: timer.php:42