PHPDaemon\Utils\DateTime::diffAsText PHP Method

diffAsText() public static method

Calculates a difference between two dates
See also: http://www.php.net/manual/en/datetime.diff.php
public static diffAsText ( integer | string | DateTimeInterface $datetime1, integer | string | DateTimeInterface $datetime2, boolean $absolute = false ) : string
$datetime1 integer | string | DateTimeInterface
$datetime2 integer | string | DateTimeInterface
$absolute boolean
return string Something like this: 1 year. 2 mon. 6 day. 4 hours. 21 min. 10 sec.
    public static function diffAsText($datetime1, $datetime2, $absolute = false)
    {
        if (!$datetime1 instanceof \DateTimeInterface) {
            $datetime1 = new static($datetime1);
        }
        if (!$datetime2 instanceof \DateTimeInterface) {
            $datetime2 = new static($datetime2);
        }
        $interval = $datetime1->diff($datetime2, $absolute);
        $str = '';
        $str .= $interval->y ? $interval->y . ' year. ' : '';
        $str .= $interval->m ? $interval->m . ' mon. ' : '';
        $str .= $interval->d ? $interval->d . ' day. ' : '';
        $str .= $interval->h ? $interval->h . ' hour. ' : '';
        $str .= $interval->i ? $interval->i . ' min. ' : '';
        $str .= $interval->s || $str === '' ? $interval->s . ' sec. ' : '';
        return rtrim($str);
    }

Usage Example

    /**
     * Called when request iterated.
     * @return integer Status.
     */
    public function run()
    {
        $stime = microtime(true);
        $this->header('Content-Type: text/html; charset=utf-8');
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
            <title>Server status.</title>
        </head>
        <body>
        <br/>Uptime: <b><?php 
        echo DateTime::diffAsText(Daemon::$startTime, time());
        ?>
</b>
        <br/><br/><b>State of workers:</b><?php 
        $stat = Daemon::getStateOfWorkers();
        ?>
        <br/>Idle: <?php 
        echo $stat['idle'];
        ?>
        <br/>Busy: <?php 
        echo $stat['busy'];
        ?>
        <br/>Total alive: <?php 
        echo $stat['alive'];
        ?>
        <br/>Shutdown: <?php 
        echo $stat['shutdown'];
        ?>
        <br/>Pre-init: <?php 
        echo $stat['preinit'];
        ?>
        <br/>Wait-init: <?php 
        echo $stat['waitinit'];
        ?>
        <br/>Init: <?php 
        echo $stat['init'];
        ?>
        <br/>
        <br/>Request took: <?php 
        printf('%f', round(microtime(true) - $stime, 6));
        ?>
        </body>
        </html>
        <?php 
    }
All Usage Examples Of PHPDaemon\Utils\DateTime::diffAsText