Answer the question
In order to leave comments, you need to log in
How to calculate the time after which the last action was?
Hey!
I am writing a small dashboard with a temperature logger and, for the sake of beauty, I displayed the date / time of the last data update. I look at it and understand that it is absolutely not visual. It is necessary to calculate how many "minutes ago" the update occurred.
Kick on the algorithm, please. By the morning I would like to finish.
Answer the question
In order to leave comments, you need to log in
<?php
// онлайн перевести дату в UNIX: http://i-leon.ru/tools/time
function showDate( $date ) // $date --> время в формате Unix time
{
$stf = 0;
$cur_time = time();
$diff = $cur_time - $date;
$seconds = array( 'секунда', 'секунды', 'секунд' );
$minutes = array( 'минута', 'минуты', 'минут' );
$hours = array( 'час', 'часа', 'часов' );
$days = array( 'день', 'дня', 'дней' );
$weeks = array( 'неделя', 'недели', 'недель' );
$months = array( 'месяц', 'месяца', 'месяцев' );
$years = array( 'год', 'года', 'лет' );
$decades = array( 'десятилетие', 'десятилетия', 'десятилетий' );
$phrase = array( $seconds, $minutes, $hours, $days, $weeks, $months, $years, $decades );
$length = array( 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( $no, $phrase[ $i ] ) );
if ( ( $stf == 1 ) && ( $i >= 1 ) && ( ( $cur_time - $_time ) > 0 ) ) {
$value .= time_ago( $_time );
}
return $value;
}
function getPhrase( $number, $titles ) {
$cases = array( 2, 0, 1, 1, 1, 2 );
return $titles[ ( $number % 100 > 4 && $number % 100 < 20 ) ? 2 : $cases[ min( $number % 10, 5 ) ] ];
}
echo "Обновлено ".showDate("1570755991")." назад";
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question