Last active
December 17, 2015 15:19
-
-
Save KLicheR/5630407 to your computer and use it in GitHub Desktop.
Get different time properties for a certain amount of seconds and help to format these properties like the PHP "date" function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TotalTime | |
{ | |
public function __construct($seconds) | |
{ | |
$this->total_seconds = (int) $seconds; | |
$this->_getTime(); | |
} | |
/** | |
* Return a formatted time. | |
* | |
* Inspired by the "date" function (https://github.com/php/php-src/blob/master/ext/date/php_date.c). | |
* | |
* @param string $format | |
* The format of the outputted time string. See the formatting options below. | |
* -- years | |
* y: The number of years. | |
* -- days | |
* e: The total number of days. | |
* d: The number of days. | |
* -- time, hours | |
* u: The total number of hours. | |
* h: The number of hours. | |
* H: The number of hours with leading zero. | |
* -- time, minutes | |
* j: The total number of minutes. | |
* m: The number of minutes. | |
* M: The number of minutes with leading zero. | |
* -- time, seconds | |
* s: The number of seconds. | |
* S: The number of seconds with leading zero. | |
* @param int $seconds | |
* The number of seconds to convert. | |
* | |
* @return string | |
* The formatted time string. | |
*/ | |
public function format($format) | |
{ | |
$ret = ''; | |
$format_len = strlen($format); | |
for ($k=0;$k<$format_len;$k++) { | |
switch ($format[$k]) { | |
/* years */ | |
case 'y':$ret .= sprintf("%d", (int) $this->years); break; | |
/* days */ | |
case 'e':$ret .= sprintf("%d", (int) $this->total_days); break; | |
case 'd':$ret .= sprintf("%d", (int) $this->days); break; | |
/* time */ | |
case 'u':$ret .= sprintf("%d", (int) $this->total_hours); break; | |
case 'h':$ret .= sprintf("%d", (int) $this->hours); break; | |
case 'H':$ret .= sprintf("%02d", (int) $this->hours); break; | |
case 'j':$ret .= sprintf("%d", (int) $this->total_minutes); break; | |
case 'm':$ret .= sprintf("%d", (int) $this->minutes); break; | |
case 'M':$ret .= sprintf("%02d", (int) $this->minutes); break; | |
case 's':$ret .= sprintf("%d", (int) $this->seconds); break; | |
case 'S':$ret .= sprintf("%02d", (int) $this->seconds); break; | |
default: $ret .= $format[$k]; break; | |
} | |
} | |
return $ret; | |
} | |
private function _getTime() | |
{ | |
$aMinute = 60; | |
$anHour = 60*$aMinute; | |
$aDay = 24*$anHour; | |
$aYear = 365.25*$aDay; | |
$mathFn = ($this->total_seconds<0?'ceil':'floor'); | |
$this->years = $mathFn($this->total_seconds / $aYear); | |
$this->days = $mathFn(($this->total_seconds % $aYear) / $aDay); | |
$this->hours = $mathFn((($this->total_seconds % $aYear) % $aDay) / $anHour); | |
$this->minutes = $mathFn(((($this->total_seconds % $aYear) % $aDay) % $anHour) / $aMinute); | |
$this->seconds = $mathFn(((($this->total_seconds % $aYear) % $aDay) % $anHour) % $aMinute); | |
$this->total_days = $mathFn($this->total_seconds / $aDay); | |
$this->total_hours = $mathFn($this->total_seconds / $anHour); | |
$this->total_minutes = $mathFn($this->total_seconds / $aMinute); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment