Example #1
0
 /**
  * Returns a UI-facing timestamp for a given {@link DateTime} object.
  *
  * - If the date/time is from today, only the time will be retuned in a localized format (e.g. “10:00 AM”).
  * - If the date/time is from yesterday, “Yesterday” will be returned.
  * - If the date/time is from the last 7 days, the name of the day will be returned (e.g. “Monday”).
  * - Otherwise, the date will be returned in a localized format (e.g. “12/2/2014”).
  *
  * @param DateTime $dateTime The DateTime object to be formatted.
  *
  * @return string The timestamp.
  */
 public static function uiTimestamp(DateTime $dateTime)
 {
     // If it's today, just return the local time.
     if (static::isToday($dateTime->getTimestamp())) {
         return $dateTime->localeTime();
     } else {
         if (static::wasYesterday($dateTime->getTimestamp())) {
             return Craft::t('Yesterday');
         } else {
             if (static::wasWithinLast('7 days', $dateTime->getTimestamp())) {
                 return Craft::t($dateTime->format('l'));
             } else {
                 // Otherwise, just return the local date.
                 return $dateTime->localeDate();
             }
         }
     }
 }