<?php
$now = time();
$yesterday = (new DateTime)->sub(new DateInterval('P1D'));
$messages =
[
// Yesteerday's messages
[
'user_id' => 1,
'user_natural_name' => 'me',
'timestamp' => $yesterday->setTime(19, 0)->format('U'),
'text' => 'Hiya'
],
[
'user_id' => 2,
'user_natural_name' => 'you',
'timestamp' => $yesterday->setTime(19, 5)->format('U'),
'text' => 'Hey, how are you?'
],
// Today's messages
[
'user_id' => 1,
'user_natural_name' => 'me',
'timestamp' => $now,
'text' => 'Hello'
],
[
'user_id' => 2,
'user_natural_name' => 'you',
'timestamp' => $now + 3,
'text' => 'Hi'
],
[
'user_id' => 1,
'user_natural_name' => 'me',
'timestamp' => $now + 6,
'text' => 'What\'s up'
],
[
'user_id' => 2,
'user_natural_name' => 'you',
'timestamp' => $now + 9,
'text' => 'Not much'
],
[
'user_id' => 1,
'user_natural_name' => 'me',
'timestamp' => $now + 12,
'text' => 'Awesome'
]
];
$lastWindowDate = NULL;
foreach ($messages as $index => $message)
{
$windowDate = date('Ymd', $message['timestamp']);
if ($windowDate !== $lastWindowDate)
{
switch (date('Ymd', $message['timestamp']))
{
case date('Ymd'):
$dateLabel = 'today';
break;
case $yesterday->format('Ymd'):
$dateLabel = 'yesterday';
break;
default:
$dateLabel = $windowDate;
break;
}
if ($index)
echo "\n";
echo "$dateLabel\n";
$lastWindowDate = $windowDate;
}
echo "{$message['user_natural_name']}: " . date('H:i', $message['timestamp']) . " - {$message['text']}\n";
}
- Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.30, 8.0.0 - 8.0.21, 8.1.0 - 8.1.8
- yesterday
me: 19:00 - Hiya
you: 19:05 - Hey, how are you?
today
me: 18:36 - Hello
you: 18:36 - Hi
me: 18:36 - What's up
you: 18:36 - Not much
me: 18:36 - Awesome
preferences:
84.77 ms | 403 KiB | 134 Q