- finally reviewed #1361;

This commit is contained in:
Mykyta Synelnikov
2023-12-01 12:50:13 +02:00
parent da5a4c869b
commit 05928088d8
+26 -38
View File
@@ -1,26 +1,18 @@
<?php
namespace um\core;
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'um\core\Date_Time' ) ) {
/**
* Class Date_Time
* @package um\core
*/
class Date_Time {
/**
* Date_Time constructor.
*/
function __construct() {
}
/**
* Display time in specific format
*
@@ -28,20 +20,21 @@ if ( ! class_exists( 'um\core\Date_Time' ) ) {
*
* @return int|string
*/
function get_time( $format ) {
public function get_time( $format ) {
return current_time( $format );
}
/**
* Show a cool time difference between 2 timestamps
*
* @todo compare this function with WordPress native `human_time_diff()` and matbe refactored it.
*
* @param int $from
* @param int $to
*
* @return string
*/
function time_diff( $from, $to = '' ) {
public function time_diff( $from, $to = '' ) {
$since = '';
if ( empty( $to ) ) {
@@ -80,14 +73,13 @@ if ( ! class_exists( 'um\core\Date_Time' ) ) {
$days = 1;
}
if ( $days == 1 ) {
if ( 1 === $days ) {
// translators: %s: time.
$since = sprintf( __( 'Yesterday at %s', 'ultimate-member' ), date_i18n( get_option( 'time_format' ), $from ) );
} else {
// translators: %1$s is a date; %2$s is a time.
$since = sprintf( __( '%1$s at %2$s', 'ultimate-member' ), date_i18n( 'F d', $from ), date_i18n( get_option( 'time_format' ), $from ) );
}
} elseif ( $diff < 30 * DAY_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
// translators: %1$s is a date; %2$s is a time.
@@ -131,40 +123,35 @@ if ( ! class_exists( 'um\core\Date_Time' ) ) {
return apply_filters( 'um_human_time_diff', $since, $diff, $from, $to );
}
/**
* Get age
* Get age.
*
* @param $then
* @todo working with timestamps in this function.
*
* @param string $then
*
* @return string
*/
function get_age( $then ) {
public function get_age( $then ) {
if ( ! $then ) {
return '';
}
$then_ts = strtotime( $then );
$then_ts = strtotime( $then );
$then_year = date( 'Y', $then_ts );
$age = date( 'Y' ) - $then_year;
$age = date( 'Y' ) - $then_year;
if ( strtotime( '+' . $age . ' years', $then_ts ) > current_time( 'timestamp' ) ) {
$age--;
}
if ( $age == 1 ) {
// translators: %s: age.
return sprintf( __( '%s year old', 'ultimate-member' ), $age );
}
if ( $age > 1 ) {
// translators: %s: age.
return sprintf( __( '%s years old', 'ultimate-member' ), $age );
}
if ( $age == 0 ) {
if ( 0 === $age ) {
return __( 'Less than 1 year old', 'ultimate-member' );
}
return '';
// translators: %s: age.
return sprintf( _n( '%s year old', '%s years old', $age, 'ultimate-member' ), $age );
}
/**
* Reformat dates
*
@@ -173,22 +160,24 @@ if ( ! class_exists( 'um\core\Date_Time' ) ) {
*
* @return string
*/
function format( $old, $new ) {
public function format( $old, $new ) {
$datetime = new \DateTime( $old );
$output = $datetime->format( $new );
$output = $datetime->format( $new );
return $output;
}
/**
* Get last 30 days as array
*
* @deprecated 2.8.0
*
* @param int $num
* @param bool $reverse
*
* @return array
*/
function get_last_days( $num = 30, $reverse = true ) {
public function get_last_days( $num = 30, $reverse = true ) {
_deprecated_function( __METHOD__, '2.8.0' );
$d = array();
for ( $i = 0; $i < $num; $i++ ) {
$d[ date('Y-m-d', strtotime( '-' . $i . ' days' ) ) ] = date( 'm/d', strtotime( '-' . $i . ' days' ) );
@@ -196,6 +185,5 @@ if ( ! class_exists( 'um\core\Date_Time' ) ) {
return ( $reverse ) ? array_reverse( $d ) : $d;
}
}
}