Topic: Вывод даты как в соц.сети vk "34 seconds ago"
Две функции, используем social_date().
<?php
function social_date( $date ) // $date --> время в формате Unix time
{
$stf = 0;
$cur_time = time();
$diff = $cur_time - $date;
$seconds = [ 'second', 'seconds', 'seconds' ];
$minutes = [ 'minute', 'minutes', 'minutes' ];
$hours = [ 'hour', 'hours', 'hours' ];
$days = [ 'day', 'day', 'days' ];
$weeks = [ 'week', 'weeks', 'weeks' ];
$months = [ 'month', 'months', 'months' ];
$years = [ 'year', 'years', 'years' ];
$decades = [ 'decade', 'decades', 'decades' ];
$phrase = [ $seconds, $minutes, $hours, $days, $weeks, $months, $years, $decades ];
$length = [ 1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600 ];
for ( $i = sizeof( $length ) - 1; ( $i >= 0 ) && ( ( $no = $diff / $length[ $i ] ) <= 1 ); $i -- ) {
;
}
if ( $i < 0 ) {
$i = 0;
}
$_time = $cur_time - ( $diff % $length[ $i ] );
$no = floor( $no );
$value = sprintf( "%d %s ", $no, getPhrase_social_date( $no, $phrase[ $i ] ) );
if ( ( $stf == 1 ) && ( $i >= 1 ) && ( ( $cur_time - $_time ) > 0 ) ) {
$value .= time_ago( $_time );
}
return $value . ' ago';
}
function getPhrase_social_date( $number, $titles ) {
$cases = [ 2, 0, 1, 1, 1, 2 ];
return $titles[ ( $number % 100 > 4 && $number % 100 < 20 ) ? 2 : $cases[ min( $number % 10, 5 ) ] ];
}
Пример:
echo social_date( 1557358124 );
// 34 seconds ago