PHPMailer\PHPMailer

PHPMailer::sanitiseDateprivate staticWP 1.0

Normalise a user-supplied date into a correctly-formatted RFC 5322 date value string suitable for use in the Date header.

Accepts:

  • A DateTime{} (or \DateTimeImmutable) object
  • Any date/time string understood by PHP's DateTime constructor (RFC 5322, ISO 8601,
Unix timestamp with leading "@", natural-language strings, etc.)

Dates in the future are not permitted for email headers; if the parsed date is later than "now" the method falls back to the current time via {@see self::rfcDate()}. An empty value, a non-string/non-DateTime argument, or any value that cannot be parsed will likewise fall back to {@see self::rfcDate()}.

Method of the class: PHPMailer{}

No Hooks.

Returns

string. An RFC 5322-formatted date string

Usage

$result = PHPMailer::sanitiseDate( $date );
$date(DateTime|\DateTimeImmutable|string) (required)
The date to normalise.

PHPMailer::sanitiseDate() code WP 7.1

private static function sanitiseDate($date)
{
    try {
        //Ensure the default timezone is set properly
        date_default_timezone_set(@date_default_timezone_get());

        if ($date instanceof \DateTimeInterface) {
            $dt = $date;
        } elseif (is_string($date) && $date !== '') {
            $dt = new \DateTime($date);
        } else {
            //Empty string, null, or any unsupported type
            return self::rfcDate();
        }

        //Reject future dates — they are invalid for outgoing message headers
        if ($dt->getTimestamp() > time()) {
            return self::rfcDate();
        }

        return $dt->format(self::RFC822_DATE_FORMAT);
    } catch (\Exception $e) {
        return self::rfcDate();
    }
}