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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| function period($x, $y)
{
$now = new \DateTime($x);
$past = new \DateTime($y);
$diff = $now->diff($past);
$txt = 'Il y a ';
if ($now->format('Y-m-d His') == $past->format('Y-m-d His'))
$txt = 'Maintenant';
if ($diff->y)
$txt .= $diff->y.' an'.(($diff->y > 1) ? 's' : '');
elseif ($diff->m)
$txt .= $diff->m.' mois';
elseif ($diff->d)
{
if ($diff->d === 1)
{
if ($now->format('His') < $past->format('His'))
$txt .= ($diff->d + 1).' jours';
else
$txt = 'Hier à '.$past->format('H').'h'.$past->format('i');
}
elseif ($diff->h || $diff->i || $diff->s)
{
if ($now->format('His') < $past->format('His'))
$txt .= ($diff->d + 1).' jours';
else
$txt .= $diff->d.' jours';
}
else
$txt .= $diff->d.' jours';
}
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) ? '<b>OK</b>' : '<font color=#ff0000>FAIL</font>', '<br>';
}
test('2016-02-26 11:32:59', '2016-02-23 16:43:56', 'Il y a 3 jours'); // OK
test('2016-02-26 16:32:00', '2016-02-23 16:43:56', 'Il y a 3 jours'); // OK
test('2016-02-26 17:11:00', '2016-02-23 16:43:56', 'Il y a 3 jours'); // OK
test('2016-02-26 22:34:00', '2016-02-25 20:47:00', 'Hier à 20h47'); // OK
test('2016-02-27 00:00:00', '2016-02-25 00:00:00', 'Il y a 2 jours'); // OK
test('2016-02-27 00:20:00', '2016-02-25 20:47:18', 'Il y a 2 jours'); // OK
test('2016-02-27 20:48:19', '2016-02-27 20:48:01', 'Il y a 18 secondes'); // OK
test('2016-02-27 20:48:10', '2016-02-27 20:47:50', 'Il y a 20 secondes'); // OK
test('2016-02-27 20:20:06', '2016-02-27 20:10:06', 'Il y a 10 minutes'); // OK
test('2016-02-27 20:20:18', '2016-02-27 19:50:18', 'Il y a 30 minutes'); // OK
test('2016-02-27 19:00:00', '2016-02-27 17:00:00', 'Il y a 2 heures'); // OK
test('2016-02-27 19:04:00', '2016-02-27 17:16:00', 'Il y a 1 heure'); // OK
test('2016-02-27 19:00:04', '2016-02-27 17:00:24', 'Il y a 1 heure'); // OK
test('2016-02-27 00:47:18', '2016-02-26 21:20:00', 'Hier à 21h20'); // OK
test('2016-02-27 23:47:18', '2016-02-26 23:20:00', 'Hier à 23h20'); // OK
test('2016-02-27 00:00:00', '2016-02-26 00:00:00', 'Hier à 00h00'); // OK
test('2016-02-27 00:00:00', '2016-02-25 00:00:00', 'Il y a 2 jours'); // OK
test('2016-02-27 00:00:00', '2016-02-25 18:00:00', 'Il y a 2 jours'); // OK
test('2016-02-27 19:00:00', '2016-02-25 18:00:00', 'Il y a 2 jours'); // OK
test('2016-02-01 18:00:00', '2016-01-31 19:00:00', 'Hier à 19h00'); // OK
test('2016-02-02 18:00:00', '2016-01-31 19:00:00', 'Il y a 2 jours'); // OK
test('2016-01-01 18:00:00', '2015-12-31 19:12:00', 'Hier à 19h12'); // OK
test('2016-01-03 18:00:00', '2015-12-31 19:12:00', 'Il y a 3 jours'); // OK
test('2016-01-31 18:00:00', '2015-12-31 17:12:00', 'Il y a 1 mois'); // OK
test('2016-06-15 18:00:00', '2015-06-15 17:12:00', 'Il y a 1 an'); // OK
test('2016-06-15 15:00:00', '2015-06-15 17:12:00', 'Il y a 11 mois'); // OK
test('2016-06-15 17:04:16', '2015-06-15 17:12:02', 'Il y a 11 mois'); // OK
test('2016-06-15 17:24:16', '2014-06-15 17:12:02', 'Il y a 2 ans'); // OK
test('2016-06-15 17:24:16', '2016-06-15 17:24:16', 'Maintenant'); // OK |
Partager