Update pagex.class.php
Fix deprecated dynamic property creation and week calculation in relativeTime function - Resolved deprecated warning by avoiding dynamic property creation on DateInterval object. - Calculated weeks separately and included them correctly in the output string. - Adjusted the logic to handle weeks within the string array without creating a dynamic property. - Ensured compatibility with PHP 8.2 and higher, where dynamic properties are deprecated. This fix prevents the "Deprecated: Creation of dynamic property DateInterval::$w is deprecated" warning and correctly formats the relative time string.
This commit is contained in:
parent
4412f339cc
commit
593f5b3aed
1 changed files with 38 additions and 31 deletions
|
@ -548,13 +548,14 @@ class Page
|
|||
$past = new DateTime($this->getValue('dateRaw'));
|
||||
$elapsed = $current->diff($past);
|
||||
|
||||
$elapsed->w = floor($elapsed->d / 7);
|
||||
$elapsed->d -= $elapsed->w * 7;
|
||||
// Calculate weeks separately
|
||||
$weeks = floor($elapsed->d / 7);
|
||||
$elapsed->d -= $weeks * 7;
|
||||
|
||||
$string = array(
|
||||
'y' => 'year',
|
||||
'm' => 'month',
|
||||
'w' => 'week',
|
||||
'w' => $weeks,
|
||||
'd' => 'day',
|
||||
'h' => 'hour',
|
||||
'i' => 'minute',
|
||||
|
@ -562,7 +563,13 @@ class Page
|
|||
);
|
||||
|
||||
foreach ($string as $key => &$value) {
|
||||
if ($elapsed->$key) {
|
||||
if ($key == 'w') {
|
||||
if ($weeks > 0) {
|
||||
$value = $weeks . ' week' . ($weeks > 1 ? 's' : '');
|
||||
} else {
|
||||
unset($string[$key]);
|
||||
}
|
||||
} elseif ($elapsed->$key) {
|
||||
$value = $elapsed->$key . ' ' . $value . ($elapsed->$key > 1 ? 's' : '');
|
||||
} else {
|
||||
unset($string[$key]);
|
||||
|
|
Loading…
Reference in a new issue