Skip to content

Core\Mail\Message::date

$message->date(): ?Core\Time\DateTime

Returns the Date header of a received message, as a Core\Time\DateTime.

The date is in the time zone that the sender wrote. A message sent at 14:05 +0100 gives 14:05 with an offset of +01:00. To compare messages from different time zones, call toInstant() on each date. It returns the same moment in UTC. To show that moment on your own clock, call in() on it with your time zone.

A message without a Date header, or with a date that cannot be read, returns null.

Good to know: the sender's computer writes this date, and its clock can be wrong. For the time that a message arrived, use the time when your program received it.

The examples below read a date with its offset, show a date that cannot be read, and sort an inbox by the moment each message was sent.

Return value

Returns ?Core\Time\DateTime. The date, or null when the message has no date or the date cannot be read.

Changelog

VersionChange
0.0.6Core\Mail\Message::parse reads a raw email, such as an .eml file or the body of a webhook. It returns the headers, the text and HTML bodies and the attachments, and a broken message does not throw an error. Every value is tainted: it comes from outside your program, so you must check or escape it.

Examples

When the message was written

<?nvs
// Shows how to read the date of a received message, with the sender's time zone.
use Core\Mail\Message;
use Core\Time\DateTime;
Message $mail = Message::parse(
"From: ada@example.org\r\n"
. "Subject: Table for Friday\r\n"
. "Date: Tue, 3 Nov 2026 14:05:09 +0100\r\n"
. "\r\n"
. 'Is there a table for two?',
);
// `date` returns the time that the sender wrote, with its offset from UTC.
// This prints `2026-11-03T14:05:09+01:00[+01:00]`. `[+01:00]` is the time zone of the value.
?DateTime $sent = $mail->date();
if ($sent != null) {
echo $sent->toIso(), "\n";
// The same moment in UTC is one hour earlier. This prints `2026-11-03T13:05:09Z`.
echo $sent->toInstant()->toIso(), "\n";
}
Output
2026-11-03T14:05:09+01:00[+01:00]
2026-11-03T13:05:09Z

A date that cannot be read

<?nvs
// Shows what `date` returns when the `Date` header is missing or cannot be read.
use Core\Mail\Message;
array<string> $headers = [
"Date: Wed, 4 Nov 2026 09:30:00 -0500\r\n",
"Date: sometime next week\r\n",
'',
];
// The first date is read. The second is not a date, and the third message has no `Date` header.
// So this prints one date and then `no date` two times.
foreach ($headers as string $header) {
Message $mail = Message::parse("From: bo@example.org\r\n" . $header . "\r\nHello");
echo $mail->date()?->format('yyyy-MM-dd HH:mm') ?? 'no date', "\n";
}
Output
2026-11-04 09:30
no date
no date

Sort an inbox by date

<?nvs
// Shows how a help desk sorts messages from different time zones and shows them on its own clock.
use Core\Mail\Message;
use Core\Time\DateTime;
use Core\Time\Zone;
array<string> $inbox = [
"Subject: From Berlin\r\nDate: Tue, 3 Nov 2026 10:00:00 +0100\r\n\r\nx",
"Subject: From New York\r\nDate: Tue, 3 Nov 2026 03:30:00 -0500\r\n\r\nx",
"Subject: From Tokyo\r\nDate: Tue, 3 Nov 2026 17:15:00 +0900\r\n\r\nx",
];
class Inbox
{
// `sentAt` returns the moment a message was sent, in seconds since 1970 in UTC.
// For a message without a date, it returns 0, so that message comes first.
public static function sentAt(Message $mail): int
{
?DateTime $date = $mail->date();
return $date == null ? 0 : $date->toInstant()->toEpochSeconds();
}
}
array<Message> $messages = [];
foreach ($inbox as string $raw) {
$messages[] = Message::parse($raw);
}
array<Message> $sorted = Core\Arr::sort($messages, { by: fn(Message $mail): int => Inbox::sentAt($mail) });
// Each date is shown on the clock of the help desk in Vienna, oldest first.
// This prints `09:15 From Tokyo`, `09:30 From New York` and `10:00 From Berlin`.
foreach ($sorted as Message $mail) {
?DateTime $date = $mail->date();
string $time = $date == null ? '--:--' : $date->toInstant()->in(Zone::of('Europe/Vienna'))->format('HH:mm');
echo $time, ' ', $mail->subject(), "\n";
}
Output
09:15 From Tokyo
09:30 From New York
10:00 From Berlin