Skip to content

Commit eb2b916

Browse files
committed
Fix singulier / pluriel
1 parent b78dae3 commit eb2b916

File tree

2 files changed

+72
-60
lines changed

2 files changed

+72
-60
lines changed

desktop/php/events.php

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,39 +59,45 @@ function formatDuration($seconds)
5959
return $formattedDuration;
6060
}
6161

62-
function timeElapsedString($datetime, $full = false)
63-
{
64-
$now = new DateTime;
65-
$ago = new DateTime($datetime);
66-
$diff = $now->diff($ago);
67-
68-
$diff->w = floor($diff->d / 7);
69-
$diff->d -= $diff->w * 7;
70-
71-
$string = array(
72-
'y' => 'année',
73-
'm' => 'mois',
74-
'w' => 'semaine',
75-
'd' => 'jour',
76-
'h' => 'heure',
77-
'i' => 'minute',
78-
's' => 'seconde',
79-
);
80-
81-
foreach ($string as $k => &$v) {
82-
if ($diff->$k) {
83-
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
84-
} else {
85-
unset($string[$k]);
86-
}
62+
function timeElapsedString($datetime, $full = false)
63+
{
64+
$now = new DateTime;
65+
$ago = new DateTime($datetime);
66+
$diff = $now->diff($ago);
67+
68+
// Ajout des semaines à partir des jours
69+
$diff->w = floor($diff->d / 7);
70+
$diff->d -= $diff->w * 7;
71+
72+
// Tableau des unités de temps avec singulier/pluriel
73+
$units = [
74+
'y' => ['année', 'années'],
75+
'm' => ['mois', 'mois'],
76+
'w' => ['semaine', 'semaines'],
77+
'd' => ['jour', 'jours'],
78+
'h' => ['heure', 'heures'],
79+
'i' => ['minute', 'minutes'],
80+
's' => ['seconde', 'secondes'],
81+
];
82+
83+
$strings = [];
84+
85+
foreach ($units as $key => [$singular, $plural]) {
86+
if ($diff->$key) {
87+
$count = $diff->$key;
88+
$strings[] = $count . ' ' . ($count > 1 ? $plural : $singular);
8789
}
90+
}
8891

89-
if (!$full)
90-
$string = array_slice($string, 0, 1);
91-
return $string ? 'il y a ' . implode(', ', $string) : 'à l\'instant';
92+
if (!$full) {
93+
$strings = array_slice($strings, 0, 1);
9294
}
9395

94-
$events = frigate::showEvents();
96+
return $strings ? 'il y a ' . implode(', ', $strings) : 'à l\'instant';
97+
}
98+
99+
100+
$events = frigate::showEvents();
95101

96102
// cameras variables
97103
$selectedCameras = isset($_GET['cameras']) ? explode(',', $_GET['cameras']) : [];

desktop/php/panel.php

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,39 +99,45 @@ function formatDuration($seconds)
9999
return $formattedDuration;
100100
}
101101

102-
function timeElapsedString($datetime, $full = false)
103-
{
104-
$now = new DateTime;
105-
$ago = new DateTime($datetime);
106-
$diff = $now->diff($ago);
107-
108-
$diff->w = floor($diff->d / 7);
109-
$diff->d -= $diff->w * 7;
110-
111-
$string = array(
112-
'y' => 'année',
113-
'm' => 'mois',
114-
'w' => 'semaine',
115-
'd' => 'jour',
116-
'h' => 'heure',
117-
'i' => 'minute',
118-
's' => 'seconde',
119-
);
120-
121-
foreach ($string as $k => &$v) {
122-
if ($diff->$k) {
123-
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
124-
} else {
125-
unset($string[$k]);
126-
}
127-
}
102+
function timeElapsedString($datetime, $full = false)
103+
{
104+
$now = new DateTime;
105+
$ago = new DateTime($datetime);
106+
$diff = $now->diff($ago);
107+
108+
// Ajout des semaines à partir des jours
109+
$diff->w = floor($diff->d / 7);
110+
$diff->d -= $diff->w * 7;
111+
112+
// Tableau des unités de temps avec singulier/pluriel
113+
$units = [
114+
'y' => ['année', 'années'],
115+
'm' => ['mois', 'mois'],
116+
'w' => ['semaine', 'semaines'],
117+
'd' => ['jour', 'jours'],
118+
'h' => ['heure', 'heures'],
119+
'i' => ['minute', 'minutes'],
120+
's' => ['seconde', 'secondes'],
121+
];
122+
123+
$strings = [];
124+
125+
foreach ($units as $key => [$singular, $plural]) {
126+
if ($diff->$key) {
127+
$count = $diff->$key;
128+
$strings[] = $count . ' ' . ($count > 1 ? $plural : $singular);
129+
}
130+
}
131+
132+
if (!$full) {
133+
$strings = array_slice($strings, 0, 1);
134+
}
135+
136+
return $strings ? 'il y a ' . implode(', ', $strings) : 'à l\'instant';
137+
}
128138

129-
if (!$full)
130-
$string = array_slice($string, 0, 1);
131-
return $string ? 'il y a ' . implode(', ', $string) : 'à l\'instant';
132-
}
133139

134-
$events = frigate::showEvents();
140+
$events = frigate::showEvents();
135141

136142
// cameras variables
137143
$selectedCameras = isset($_GET['cameras']) ? explode(',', $_GET['cameras']) : [];

0 commit comments

Comments
 (0)