Scalr_Util_DateTime::getHumanReadableTimeout PHP Method

getHumanReadableTimeout() public static method

Based on the word_time() function from PG+ (http://pgplus.ewtoo.org)
public static getHumanReadableTimeout ( integer | string $time, boolean $show_secs = true ) : string
$time integer | string Timeout
$show_secs boolean optional Should it show the seconds
return string Returns human readable timeout
    public static function getHumanReadableTimeout($time = 0, $show_secs = true)
    {
        if (!is_numeric($time)) {
            $time = strtotime($time);
        }
        if ($time == 0) {
            return 'Unknown';
        } else {
            if ($time < 0) {
                $neg = 1;
                $time = 0 - $time;
            } else {
                $neg = 0;
            }
            $days = floor($time / 86400);
            $hrs = $time / 3600 % 24;
            $mins = $time / 60 % 60;
            $secs = $show_secs ? $time % 60 : 0;
            $timestring = '';
            if ($neg) {
                $timestring .= 'negative ';
            }
            if ($days) {
                $timestring .= "{$days} day" . ($days == 1 ? '' : 's');
                if ($hrs || $mins || $secs) {
                    $timestring .= ', ';
                }
            }
            if ($hrs) {
                $timestring .= "{$hrs} hour" . ($hrs == 1 ? '' : 's');
                if ($mins && $secs) {
                    $timestring .= ', ';
                }
                if ($mins && !$secs || !$mins && $secs) {
                    $timestring .= ' and ';
                }
            }
            if ($mins) {
                $timestring .= "{$mins} min" . ($mins == 1 ? '' : 's');
                if ($mins && $secs) {
                    $timestring .= ', ';
                }
                if ($secs) {
                    $timestring .= ' and ';
                }
            }
            if ($secs) {
                $timestring .= "{$secs} sec" . ($secs == 1 ? '' : 's');
            }
            return $timestring;
        }
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Update server's information
  *
  * @param jsonData $servers optional List of servers to check against
  * @throws \Scalr_Exception_InsufficientPermissions
  */
 public function xListServersUpdateAction(JsonData $servers = null)
 {
     if (!$this->request->isAllowed([Acl::RESOURCE_FARMS, Acl::RESOURCE_TEAM_FARMS, Acl::RESOURCE_OWN_FARMS]) && !$this->request->isAllowed(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE)) {
         throw new \Scalr_Exception_InsufficientPermissions();
     }
     $props = $retval = [];
     $args = $servers = (array) $servers;
     $stmt = "\n            SELECT s.server_id, s.status, s.remote_ip, s.local_ip, s.dtadded, s.dtinitialized AS uptime\n            FROM servers s\n            LEFT JOIN farms f ON f.id = s.farm_id\n            WHERE s.server_id IN (" . implode(",", array_fill(0, count($servers), "?")) . ")\n            AND s.env_id = ?\n        ";
     $args[] = $this->getEnvironmentId();
     $where = ["s.farm_id IS NOT NULL AND " . $this->request->getFarmSqlQuery()];
     if ($this->request->isAllowed(Acl::RESOURCE_IMAGES_ENVIRONMENT, Acl::PERM_IMAGES_ENVIRONMENT_MANAGE)) {
         $where[] = "s.farm_id IS NULL AND s.status IN (?, ?)";
         $args[] = Entity\Server::STATUS_IMPORTING;
         $args[] = Entity\Server::STATUS_TEMPORARY;
     }
     $stmt .= " AND (" . join(" OR ", $where) . ")";
     if (!empty($servers)) {
         $srs = $this->db->Execute($stmt, $args);
         $neededProps = [Entity\Server::REBOOTING, Entity\Server::MISSING, Entity\Server::SZR_IS_INIT_FAILED, Entity\Server::LAUNCH_ERROR, Entity\Server::SZR_VESION];
         foreach (Entity\Server\Property::fetch($servers, $neededProps) as $resRow) {
             if (!array_key_exists($resRow->serverId, $props)) {
                 $props[$resRow->serverId] = [];
             }
             $props[$resRow->serverId][$resRow->name] = $resRow->value;
         }
         while ($server = $srs->FetchRow()) {
             if (!array_key_exists($server["server_id"], $props)) {
                 $props[$server["server_id"]] = [];
             }
             $status = $server["status"];
             if (in_array($status, [Entity\Server::STATUS_RUNNING, Entity\Server::STATUS_SUSPENDED])) {
                 if (array_key_exists(Entity\Server::REBOOTING, $props[$server["server_id"]]) && $props[$server["server_id"]][Entity\Server::REBOOTING] != 0) {
                     $server["status"] = "Rebooting";
                 }
                 if (array_key_exists(Entity\Server::MISSING, $props[$server["server_id"]]) && $props[$server["server_id"]][Entity\Server::MISSING] != 0) {
                     $server["status"] = "Missing";
                 }
             }
             if (array_key_exists(Entity\Server::SZR_IS_INIT_FAILED, $props[$server["server_id"]]) && $props[$server["server_id"]][Entity\Server::SZR_IS_INIT_FAILED] == 1 && in_array($server["status"], [Entity\Server::STATUS_INIT, Entity\Server::STATUS_PENDING])) {
                 $server["isInitFailed"] = 1;
             }
             if (array_key_exists(Entity\Server::LAUNCH_ERROR, $props[$server["server_id"]]) && $props[$server["server_id"]][Entity\Server::LAUNCH_ERROR] == 1) {
                 $server["launch_error"] = "1";
             }
             $server["agent_version"] = $props[$server["server_id"]][Entity\Server::SZR_VESION];
             if ($status === Entity\Server::STATUS_RUNNING) {
                 $server['uptime'] = \Scalr_Util_DateTime::getHumanReadableTimeout(time() - strtotime($server['uptime']), false);
             } else {
                 $server['uptime'] = '';
             }
             $retval[$server["server_id"]] = $server;
         }
     }
     $this->response->data(["servers" => $retval]);
 }
All Usage Examples Of Scalr_Util_DateTime::getHumanReadableTimeout