Scalr_Util_DateTime::getFuzzyTimeString PHP Method

getFuzzyTimeString() public static method

public static getFuzzyTimeString ( $value )
    public static function getFuzzyTimeString($value)
    {
        if (is_integer($value)) {
            $value = "@{$value}";
        }
        if (!$value instanceof DateTime) {
            $value = new DateTime($value);
        }
        $time = $value->getTimestamp();
        if ($time) {
            $now = time();
            $sodTime = mktime(0, 0, 0, date('m', $time), date('d', $time), date('Y', $time));
            $sodNow = mktime(0, 0, 0, date('m', $now), date('d', $now), date('Y', $now));
            $diff = $sodNow - $sodTime;
            if ($sodNow == $sodTime) {
                // check 'today'
                return 'today at ' . Scalr_Util_DateTime::convertTz($time, 'g:ia');
            } else {
                if ($diff <= 86400) {
                    // check 'yesterday'
                    return 'yesterday at ' . Scalr_Util_DateTime::convertTz($time, 'g:ia');
                } else {
                    if ($diff <= 604800) {
                        //within last week
                        return floor($diff / 86400) . ' days ago';
                    } else {
                        if ($diff <= 2419200) {
                            //within last month
                            $week = floor($diff / 604800);
                            return $week . ' week' . ($week > 1 ? 's' : '') . ' ago';
                        } else {
                            if (date('Y', $now) == date('Y', $time)) {
                                return Scalr_Util_DateTime::convertTz($time, 'M j \\a\\t g:ia');
                                // miss off the year if it's this year
                            } else {
                                return Scalr_Util_DateTime::convertTz($time, 'M j, Y');
                                // return the date as normal
                            }
                        }
                    }
                }
            }
        } else {
            return NULL;
        }
    }

Usage Example

Example #1
0
 public function xGenerateApiKeyAction()
 {
     if ($this->user->isAdmin() || !\Scalr::config('scalr.system.api.enabled')) {
         throw new Scalr_Exception_InsufficientPermissions();
     }
     $apiKeyEntity = new ApiKeyEntity($this->user->getId());
     $apiKeyEntity->save();
     $row = get_object_vars($apiKeyEntity);
     $row['created'] = Scalr_Util_DateTime::convertTz($apiKeyEntity->created);
     $row['lastUsed'] = $apiKeyEntity->lastUsed ? Scalr_Util_DateTime::convertTz($apiKeyEntity->lastUsed) : 0;
     $row['createdHr'] = $apiKeyEntity->created ? Scalr_Util_DateTime::getFuzzyTimeString($apiKeyEntity->created) : '';
     $row['lastUsedHr'] = $apiKeyEntity->lastUsed ? Scalr_Util_DateTime::getFuzzyTimeString($apiKeyEntity->lastUsed) : 'Never';
     $this->response->data(['key' => $row]);
 }
All Usage Examples Of Scalr_Util_DateTime::getFuzzyTimeString