Benutzer-Werkzeuge

Webseiten-Werkzeuge


php:timespan

Sekunden in Tage, Stunden und Minuten zerlegen

class.timepsan.php
class timespan
{
    private $seconds    = 0;
    private $minutes    = 0;
    private $hours      = 0;
    private $days       = 0;
 
    /**
     * timespan constructor.
     *
     * @param $seconds
     */
    public function __construct( $seconds )
    {
        $this->calculate( $seconds );
    }
 
    private function calculate( $input )
    {
        $this->seconds = $input % 60;
        $this->minutes = (( $input - $this->seconds ) / 60 ) % 60;
        $this->hours = (((( $input - $this->seconds ) / 60 ) - $this->minutes ) / 60 ) % 24;
        $this->days = floor ( ((((( $input - $this->seconds ) / 60 ) - $this->minutes ) / 60 ) / 24 ) );
    }
 
    /**
     * @return mixed
     */
    public function getSeconds()
    {
        return $this->seconds;
    }
 
    /**
     * @return mixed
     */
    public function getMinutes()
    {
        return $this->minutes;
    }
 
    /**
     * @return mixed
     */
    public function getHours()
    {
        return $this->hours;
    }
 
    /**
     * @return mixed
     */
    public function getDays()
    {
        return $this->days;
    }
}

Anwendungsbeispiel:

$timespan = new timespan( 1234567 );
echo "<p>Tage: " . $timespan->getDays() . "</p>";
echo "<p>Stunden: " . $timespan->getHours() . "</p>";
echo "<p>Minuten: " . $timespan->getMinutes() . "</p>";
echo "<p>Sekunden: " . $timespan->getSeconds() . "</p>";

Ergebnis:

  • Tage: 14
  • Stunden: 6
  • Minuten: 56
  • Sekunden: 7

Gegenprobe:

  • 14 * 24 * 60 * 60 = 1209600 Sekunden
  • 6 * 60 * 60 =21600 Sekunden
  • 56 * 60 = 3360 Sekunden
  • 7 Sekunden
  • 1209600 + 21600 + 3360 + 7 = 1234567 Sekunden
php/timespan.txt · Zuletzt geändert: 2017/07/21 00:00 (Externe Bearbeitung)