FOSSology  4.4.0
Open Source License Compliance by Open Source Software
timer.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
30 class timer
31 {
32  public $startTime;
33 
34  public function __construct()
35  {
36  $this->startTime = time();
37  }
38  public function getStartTime()
39  {
40  return ($this->startTime);
41  }
54  public static function TimeAgo($timestamp)
55  {
56  // Store the current time
57  $current_time = time();
58 
59  // Determine the difference, between the time now and the timestamp
60  $difference = $current_time - $timestamp;
61 
62  // Set the periods of time
63  $periods = array (
64  "second",
65  "minute",
66  "hour",
67  "day",
68  "week",
69  "month",
70  "year",
71  "decade"
72  );
73 
74  // Set the number of seconds per period
75  $lengths = array (
76  1,
77  60,
78  3600,
79  86400,
80  604800,
81  2630880,
82  31570560,
83  315705600
84  );
85 
86  // Determine which period we should use, based on the number of seconds lapsed.
87  // 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
88  // Go from decades backwards to seconds
89  for ($val = sizeof($lengths) - 1;($val >= 0) && (($number = $difference / $lengths[$val]) <= 1); $val--);
90 
91  // Ensure the script has found a match
92  if ($val < 0)
93  $val = 0;
94 
95  // Determine the minor value, to recurse through
96  $new_time = $current_time - ($difference % $lengths[$val]);
97 
98  // Set the current value to be floored
99  $number = floor($number);
100 
101  // If required create a plural
102  if ($number != 1)
103  $periods[$val] .= "s";
104 
105  // Return text
106  $text = sprintf("%d %s ", $number, $periods[$val]);
107 
108  // Ensure there is still something to recurse through, and we have not found 1 minute and 0 seconds.
109  if (($val >= 1) && (($current_time - $new_time) > 0))
110  {
111  $text .= self :: TimeAgo($new_time);
112  }
113  return $text;
114  }
115 }
Definition: timer.php:31
static TimeAgo($timestamp)
Definition: timer.php:54