Version 4.6.0

This commit is contained in:
Neo committed 2025-12-19 05:55:31 -08:00
1 parent f79dcf067a
commit 517a5e1f70
2036 files changed
+110041 -26162

No files matched your search

+232 -92
View File
@@ -252,6 +252,30 @@ class _DateTime extends \DateTime
);
}
/**
* Format the date to return short month and full year.
*
* @param \IPS\Lang|\IPS\Member|NULL $memberOrLanguage The language or member to use, or NULL for currently logged in member
* @return string
*/
public function shortMonthAndFullYear( $memberOrLanguage=NULL )
{
$language = static::determineLanguage( $memberOrLanguage );
return $language->addToStack(
'_date_month_year_short',
FALSE,
array(
'sprintf' => array(
static::strftime( '%Y', $this->getTimestamp() + $this->getTimezone()->getOffset( $this ), $language )
),
'pluralize' => array(
static::strftime( '%m', $this->getTimestamp() + $this->getTimezone()->getOffset( $this ), $language ),
)
)
);
}
/**
* Get locale date, forced to 4-digit year format
*
@@ -354,130 +378,141 @@ class _DateTime extends \DateTime
public function relative( $format=0, $memberOrLanguage=NULL )
{
$language = static::determineLanguage( $memberOrLanguage );
$now = static::ts( time() );
$difference = $this->diff( $now );
$capitalKey = ( $format == static::RELATIVE_FORMAT_LOWER ) ? '' : '_c';
/* More than a year ago...1y is good enough for short dates */
if( $format == static::RELATIVE_FORMAT_SHORT AND !$difference->invert and $now->format( 'Y', $language ) != $this->format( 'Y', $language ) )
if( \IPS\Settings::i()->relative_dates_enable )
{
if( $difference->y )
{
return $language->addToStack( 'f_years_short', FALSE, array( 'pluralize' => array( $difference->y ) ) );
}
else
{
return $language->addToStack(
'_date_this_year_short',
FALSE,
array(
'pluralize' => array(
static::strftime( ( strftime( '%d' ) !== FALSE ) ? '%d' : '%e', $this->getTimestamp() + $this->getTimezone()->getOffset( $this ), $language ),
static::strftime( '%m', $this->getTimestamp() + $this->getTimezone()->getOffset( $this ), $language ),
)
)
);
}
}
$now = static::ts( time() );
$difference = $this->diff( $now );
$capitalKey = ( $format == static::RELATIVE_FORMAT_LOWER ) ? '' : '_c';
/* In the past and from this year */
if ( !$difference->invert and $now->format( 'Y', $language ) == $this->format( 'Y', $language ) )
{
/* More than a week ago: "March 4" */
if ( $difference->m or $difference->d >= 6 )
/* More than a year ago...1y is good enough for short dates */
if( $format == static::RELATIVE_FORMAT_SHORT AND !$difference->invert and $now->format( 'Y', $language ) != $this->format( 'Y', $language ) )
{
return $language->addToStack(
$format == static::RELATIVE_FORMAT_SHORT ? '_date_this_year_short' : '_date_this_year_long',
FALSE,
array(
'pluralize' => array(
static::strftime( ( strftime( '%d' ) !== FALSE ) ? '%d' : '%e', $this->getTimestamp() + $this->getTimezone()->getOffset( $this ), $language ),
static::strftime( '%m', $this->getTimestamp() + $this->getTimezone()->getOffset( $this ), $language ),
)
)
);
}
/* Less than a week but more than a day ago */
elseif ( $difference->d )
{
$compare = clone $this;
/* Short format: "1 dy" */
if ( $format === static::RELATIVE_FORMAT_SHORT )
if( $difference->y )
{
return $language->addToStack( 'f_days_short', FALSE, array( 'pluralize' => array( $difference->d ) ) );
return $language->addToStack( 'f_years_short', FALSE, array( 'pluralize' => array( $difference->y ) ) );
}
/* Yesterday: "Yesterday at 23:56" */
elseif ( $difference->d == 1 && ( $compare->add( new \DateInterval( 'P1D' ) )->format( 'Y-m-d', $language ) == $now->format( 'Y-m-d', $language ) ) )
{
return $language->addToStack( "_date_yesterday{$capitalKey}", FALSE, array( 'sprintf' => array( $this->localeTime( FALSE, TRUE, $language ) ) ) );
}
/* Other: "Wednesday at 23:56" */
else
{
return $language->addToStack(
"_date_this_week{$capitalKey}",
'_date_this_year_short',
FALSE,
array(
'sprintf' => array(
$this->localeTime( FALSE, TRUE, $language )
),
'pluralize' => array(
$this->strFormat( '%w', $language ),
static::strftime( ( strftime( '%d' ) !== FALSE ) ? '%d' : '%e', $this->getTimestamp() + $this->getTimezone()->getOffset( $this ), $language ),
static::strftime( '%m', $this->getTimestamp() + $this->getTimezone()->getOffset( $this ), $language ),
)
)
);
}
}
/* Less than a day but more than an hour ago */
elseif ( $difference->h )
/* In the past and from this year */
if ( !$difference->invert and $now->format( 'Y', $language ) == $this->format( 'Y', $language ) )
{
/* Short format: "1 hr" */
if ( $format == static::RELATIVE_FORMAT_SHORT )
/* More than a week ago: "March 4" */
if ( $difference->m or $difference->d >= 6 )
{
return $language->addToStack( 'f_hours_short', FALSE, array( 'pluralize' => array( $difference->h ) ) );
return $language->addToStack(
$format == static::RELATIVE_FORMAT_SHORT ? '_date_this_year_short' : '_date_this_year_long',
FALSE,
array(
'pluralize' => array(
static::strftime( ( strftime( '%d' ) !== FALSE ) ? '%d' : '%e', $this->getTimestamp() + $this->getTimezone()->getOffset( $this ), $language ),
static::strftime( '%m', $this->getTimestamp() + $this->getTimezone()->getOffset( $this ), $language ),
)
)
);
}
/* Long format: "1 hour ago" */
/* Less than a week but more than a day ago */
elseif ( $difference->d )
{
$compare = clone $this;
/* Short format: "1 dy" */
if ( $format === static::RELATIVE_FORMAT_SHORT )
{
return $language->addToStack( 'f_days_short', FALSE, array( 'pluralize' => array( $difference->d ) ) );
}
/* Yesterday: "Yesterday at 23:56" */
elseif ( $difference->d == 1 && ( $compare->add( new \DateInterval( 'P1D' ) )->format( 'Y-m-d', $language ) == $now->format( 'Y-m-d', $language ) ) )
{
return $language->addToStack( "_date_yesterday{$capitalKey}", FALSE, array( 'sprintf' => array( $this->localeTime( FALSE, TRUE, $language ) ) ) );
}
/* Other: "Wednesday at 23:56" */
else
{
return $language->addToStack(
"_date_this_week{$capitalKey}",
FALSE,
array(
'sprintf' => array(
$this->localeTime( FALSE, TRUE, $language )
),
'pluralize' => array(
$this->strFormat( '%w', $language ),
)
)
);
}
}
/* Less than a day but more than an hour ago */
elseif ( $difference->h )
{
/* Short format: "1 hr" */
if ( $format == static::RELATIVE_FORMAT_SHORT )
{
return $language->addToStack( 'f_hours_short', FALSE, array( 'pluralize' => array( $difference->h ) ) );
}
/* Long format: "1 hour ago" */
else
{
return $language->addToStack( "_date_ago{$capitalKey}", FALSE, array( 'sprintf' => array( $language->addToStack( 'f_hours', FALSE, array( 'pluralize' => array( $difference->h ) ) ) ) ) );
}
}
/* Less than an hour but more than a minute ago */
elseif ( $difference->i )
{
/* Short format: "4 min" */
if ( $format == static::RELATIVE_FORMAT_SHORT )
{
return $language->addToStack( 'f_minutes_short', FALSE, array( 'pluralize' => array( $difference->i ) ) );
}
/* Short format: "4 minutes ago" */
else
{
return $language->addToStack( "_date_ago{$capitalKey}", FALSE, array( 'sprintf' => array( $language->addToStack( 'f_minutes', FALSE, array( 'pluralize' => array( $difference->i ) ) ) ) ) );
}
}
/* Less than a minute ago */
else
{
return $language->addToStack( "_date_ago{$capitalKey}", FALSE, array( 'sprintf' => array( $language->addToStack( 'f_hours', FALSE, array( 'pluralize' => array( $difference->h ) ) ) ) ) );
}
}
/* Less than an hour but more than a minute ago */
elseif ( $difference->i )
{
/* Short format: "4 min" */
if ( $format == static::RELATIVE_FORMAT_SHORT )
{
return $language->addToStack( 'f_minutes_short', FALSE, array( 'pluralize' => array( $difference->i ) ) );
}
/* Short format: "4 minutes ago" */
else
{
return $language->addToStack( "_date_ago{$capitalKey}", FALSE, array( 'sprintf' => array( $language->addToStack( 'f_minutes', FALSE, array( 'pluralize' => array( $difference->i ) ) ) ) ) );
}
}
/* Less than a minute ago */
else
{
if ( $format == static::RELATIVE_FORMAT_SHORT )
{
return $language->addToStack( 'f_minutes_short', FALSE, array( 'pluralize' => array( 1 ) ) );
}
else
{
return $language->addToStack( "_date_just_now{$capitalKey}" );
if ( $format == static::RELATIVE_FORMAT_SHORT )
{
return $language->addToStack( 'f_minutes_short', FALSE, array( 'pluralize' => array( 1 ) ) );
}
else
{
return $language->addToStack( "_date_just_now{$capitalKey}" );
}
}
}
$_format = ( $format == static::RELATIVE_FORMAT_SHORT ) ? '_date_last_year_short' : '_date_last_year_long';
}
else
{
$_format = ( $format == static::RELATIVE_FORMAT_SHORT ) ? '_date_last_year_short' : '_date_last_year_long_full';
}
/* Anything else - "March 4, 1992" */
return $language->addToStack(
$format == static::RELATIVE_FORMAT_SHORT ? '_date_last_year_short' : '_date_last_year_long',
$_format,
FALSE,
array(
'sprintf' => array(
static::strftime( '%Y', $this->getTimestamp() + $this->getTimezone()->getOffset( $this ), $language ),
$this->localeTime( FALSE, TRUE, $language )
),
'pluralize' => array(
static::strftime( ( strftime( '%d' ) !== FALSE ) ? '%d' : '%e', $this->getTimestamp() + $this->getTimezone()->getOffset( $this ), $language ),
@@ -590,4 +625,109 @@ class _DateTime extends \DateTime
return $result;
}
/**
* Show a datetime diff rounded for human readability
* e.g. "4 days, 5 hours"
*
* @param \IPS\DateTime $date Date to compare against
* @param \IPS\Lang|\IPS\Member|NULL $memberOrLanguage The language or member to use, or NULL for currently logged in member
* @return string
*/
public function roundedDiff( \IPS\DateTime $date, $memberOrLanguage=NULL )
{
$language = static::determineLanguage( $memberOrLanguage );
$difference = $this->diff( $date );
/* More than a year ago... */
if( $difference->y )
{
return $language->addToStack( 'dtdiff_years', FALSE, array( 'pluralize' => array( $difference->y ) ) );
}
/* Less than a year ago but more than a month ago... */
if ( $difference->m )
{
/* Do we have days to include? */
if ( $difference->d )
{
/* If there are more than 12 hours, round up an extra day */
$days = ( $difference->h > 12 ) ? $difference->d + 1 : $difference->d;
return $language->addToStack( 'dtdiff_months_days', FALSE, array( 'pluralize' => array( $difference->m, $days ) ) );
}
/* Month(s) ago with no additional days */
else
{
return $language->addToStack( 'dtdiff_months', FALSE, array( 'pluralize' => array( $difference->m ) ) );
}
}
/* Less than a month ago but more than a day ago... */
if ( $difference->d )
{
/* Do we have hours to include? */
if ( $difference->h )
{
/* If there are more than 30 minutes, round up an extra hour */
$hours = ( $difference->i > 30 ) ? $difference->h + 1 : $difference->h;
return $language->addToStack( 'dtdiff_days_hours', FALSE, array( 'pluralize' => array( $difference->d, $hours ) ) );
}
/* Day(s) ago with no additional hours */
else
{
return $language->addToStack( 'dtdiff_days', FALSE, array( 'pluralize' => array( $difference->d ) ) );
}
}
/* Less than a day ago but more than an hour ago... */
if ( $difference->h )
{
/* Do we have minutes to include? */
if ( $difference->i )
{
/* If there are more than 30 seconds, round up an extra minute */
$minutes = ( $difference->s > 30 ) ? $difference->i + 1 : $difference->i;
return $language->addToStack( 'dtdiff_hours_minutes', FALSE, array( 'pluralize' => array( $difference->h, $minutes ) ) );
}
/* Hour(s) ago with no additional minutes*/
else
{
return $language->addToStack( 'dtdiff_hours', FALSE, array( 'pluralize' => array( $difference->h ) ) );
}
}
/* Less than an hour ago but more than a minute ago... */
if( $difference->i )
{
$minutes = ( $difference->s > 30 ) ? $difference->i + 1 : $difference->i;
return $language->addToStack( 'dtdiff_minutes', FALSE, array( 'pluralize' => array( $minutes ) ) );
}
/* Less than a minute ago but more than a second ago... */
if( $difference->s )
{
return $language->addToStack( 'dtdiff_seconds', FALSE, array( 'pluralize' => array( $difference->s ) ) );
}
return $language->addToStack( 'dtdiff_none', FALSE );
}
/**
* Show a datetime diff rounded for human readability based purely on a number of seconds
* e.g. "4 days, 5 hours"
*
* @param int $seconds Number of seconds difference
* @param \IPS\Lang|\IPS\Member|NULL $memberOrLanguage The language or member to use, or NULL for currently logged in member
* @return string
*/
public static function roundedDiffFromSeconds( $seconds, $memberOrLanguage=NULL )
{
$now = static::ts( time() );
$then = static::ts( time() - $seconds );
return $now->roundedDiff( $then, $memberOrLanguage );
}
}