<?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";
}
preferences:
45.39 ms | 405 KiB | 5 Q