1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| function period($x, $y)
{
$now = new \DateTime($x);
$past = new \DateTime($y);
$diff = $now->diff($past);
$txt = 'Il y a ';
if ($diff->y)
$txt .= $diff->y.' an'.(($diff->y > 1) ? 's' : '');
elseif ($diff->m)
$txt .= $diff->m.' mois';
elseif ($diff->d)
{
if ($diff->h)
$txt .= ($diff->d + 1).' jours';
else
$txt = 'Hier à '.$past->format('H').'h'.$past->format('i');
}
else
{
if ($now->format('d') !== $past->format('d'))
$txt = 'Hier à '.$past->format('H').'h'.$past->format('i');
elseif ($diff->h)
$txt .= $diff->h.' heure'.(($diff->h > 1) ? 's' : '');
elseif ($diff->i)
$txt .= $diff->i.' minute'.(($diff->i > 1) ? 's' : '');
elseif ($diff->s)
$txt .= $diff->s.' seconde'.(($diff->s > 1) ? 's' : '');
}
return $txt;
}
function test($d1, $d2, $assert)
{
$x = period($d1, $d2);
echo $x, ' | ', $assert, ' --- ', 'Passed : ', ($x === $assert) ? 'OK' : 'FAIL', '<br>';
}
test('2016-02-25 19:11:00', '2016-02-25 19:10:35', 'Il y a 25 secondes');
test('2016-02-25 19:11:00', '2016-02-25 19:00:35', 'Il y a 10 minutes');
test('2016-02-25 19:11:00', '2016-02-24 19:00:35', 'Hier à 19h00'); |
Partager