options; $blog_timezone = new \DateTimeZone( $o2_options['options']['blogTimezoneString'] ); $now = new \DateTime(); $now->setTimezone( $blog_timezone ); $given_date = new \DateTime( $date_string_utc, new \DateTimeZone( 'UTC' ) ); $given_date->setTimezone( $blog_timezone ); $diff = $now->diff( $given_date ); if ( $diff->days > 0 ) { if ( $now->format( 'Y' ) === $given_date->format( 'Y' ) ) { return $given_date->format( 'M d' ); } return $given_date->format( ' M d, Y' ); } if ( $diff->h > 0 ) { return $diff->h . _x( 'h ago', 'Post publish date (hours since)', 'p2020' ); } if ( $diff->i < 5 ) { return _x( 'Just now', 'Post publish date', 'p2020' ); } else { return $diff->i . _x( 'm ago', 'Post publish date (minutes since)', 'p2020' ); } } /** * PHP implementation of moment.fromNow, but only of a difference of < 1 day * @param $diff * * @return string|void */ function from_now( $diff ) { // Just display the largest relative unit if ( $diff->h > 0 ) { if ( $diff->i > 29 ) { $hours = floor( ( $diff->h * 60 + $diff->i ) / 60 ); } else { $hours = $diff->h; } /* Translators: %s is a number */ return sprintf( _nx( '%s hour ago', '%s hours ago', $hours, 'time ago in hours', 'p2020' ), number_format_i18n( $hours ) ); } else { if ( $diff->i > 44 ) { return __( 'an hour ago', 'p2020' ); } if ( ( 1 === $diff->i && $diff->s > 29 ) || $diff->i > 1 ) { $minutes = floor( ( $diff->i * 60 + $diff->s ) / 60 ); /* Translators: %s is a number */ return sprintf( _nx( '%s min ago', '%s mins ago', $minutes, 'time ago in minutes', 'p2020' ), number_format_i18n( $minutes ) ); } if ( $diff->s > 44 ) { return __( 'a minute ago', 'p2020' ); } if ( $diff->s > 10 ) { /* Translators: %s is a number */ return sprintf( _x( '%s seconds ago', 'time ago in seconds', 'p2020' ), $diff->s ); } } return __( 'a few seconds ago', 'p2020' ); } /** * Get human friendly date for default posts PHP version of Utilities.timestamp * * @param string $date_string_utc * @param bool $compact_allowed * @return string */ function default_human_date( $date_string_utc, $compact_allowed ) { global $o2; $o2_options = $o2->options; $blog_timezone = new \DateTimeZone( $o2_options['options']['blogTimezoneString'] ); $now = new \DateTime(); $now->setTimezone( $blog_timezone ); $yesterday = new \DateTime( 'yesterday' ); $yesterday->setTimezone( $blog_timezone ); $given_date = new \DateTime( $date_string_utc, new \DateTimeZone( 'UTC' ) ); $given_date->setTimezone( $blog_timezone ); $diff = $now->diff( $given_date ); // Handle compact time formats $compact = wp_is_mobile(); // Compact versus long-form if ( $compact && $compact_allowed ) { // Just display the largest relative unit if ( $diff->y > 0 ) { return sprintf( $o2_options['options']['compactFormat']['years'], $diff->y ); } if ( $diff->m > 0 ) { return sprintf( $o2_options['options']['compactFormat']['months'], $diff->m ); } if ( $diff->d > 7 ) { $weeks = round( $diff->d / 7 ); return sprintf( $o2_options['options']['compactFormat']['weeks'], $weeks ); } if ( $diff->d > 0 ) { return sprintf( $o2_options['options']['compactFormat']['days'], $diff->d ); } if ( $diff->h > 0 ) { return sprintf( $o2_options['options']['compactFormat']['hours'], $diff->h ); } if ( $diff->i > 0 ) { return sprintf( $o2_options['options']['compactFormat']['minutes'], $diff->i ); } return sprintf( $o2_options['options']['compactFormat']['seconds'], $diff->s ); } if ( $given_date->format( 'Y-m-d' ) === $now->format( 'Y-m-d' ) ) { return from_now( $diff ); } if ( $given_date->format( 'Y-m-d' ) === $yesterday->format( 'Y-m-d' ) ) { // Display a relative time yesterday return sprintf( $o2_options['options']['yesterdayFormat'], $given_date->format( $o2_options['options']['timeFormat'] ) ); } if ( 0 === $diff->days ) { if ( 0 === $diff->h && 0 === $diff->i ) { // seconds ago return sprintf( $o2_options['options']['compactFormat']['seconds'], $diff->s ); } // Display a relative time today return from_now( $diff ); } // Just display a time return sprintf( $o2_options['options']['timestampFormat'], $given_date->format( $o2_options['options']['timeFormat'] ), $given_date->format( $o2_options['options']['dateFormat'] ) ); }