Flarum\Event\PrepareApiAttributes::formatDate PHP Method

formatDate() public method

public formatDate ( DateTime $date = null ) : string | null
$date DateTime
return string | null
    public function formatDate(DateTime $date = null)
    {
        if ($date) {
            return $date->format(DateTime::RFC3339);
        }
    }

Usage Example

 /**
  * @param PrepareApiAttributes $event
  */
 public function addAttributes(PrepareApiAttributes $event)
 {
     if ($event->isSerializer(UserSerializer::class)) {
         $canSuspend = $event->actor->can('suspend', $event->model);
         if ($canSuspend) {
             $event->attributes['suspendUntil'] = $event->formatDate($event->model->suspend_until);
         }
         $event->attributes['canSuspend'] = $canSuspend;
     }
 }