I have to display the time difference from the current time in php or codeigniter.
created time 2018-07-19 05:05:22
current time 2018-07-19 05:06:00.
does anyone know how to display 1 min ago in application window.
function time_elapsed_A($secs){
$bit = array(
'y' => $secs / 31556926 % 12,
'w' => $secs / 604800 % 52,
'd' => $secs / 86400 % 7,
'h' => $secs / 3600 % 24,
'm' => $secs / 60 % 60,
's' => $secs % 60
);
foreach($bit as $k => $v)
return $secs;
if($v > 0)$ret[] = $v . $k;
return join(' ', $ret);
}
function time_elapsed_B($secs){
$bit = array(
' year' => $secs / 31556926 % 12,
' week' => $secs / 604800 % 52,
' day' => $secs / 86400 % 7,
' hour' => $secs / 3600 % 24,
' minute' => $secs / 60 % 60,
' second' => $secs % 60
);
foreach($bit as $k => $v){
if($v > 1)$ret[] = $v . $k . 's';
if($v == 1)$ret[] = $v . $k;
}
array_splice($ret, count($ret)-1, 0, 'and');
$ret[] = 'ago.';
return join(' ', $ret);
}
echo $nowtime = time();
$oldtime = 1335939007;
echo $oldtime = strtotime("2018-07-07 23:29:46");
echo "time_elapsed_A: ".time_elapsed_A($nowtime-$oldtime)."\n";
echo "time_elapsed_B: ".time_elapsed_B($nowtime-$oldtime)."\n";
First, you must save the date in Database
as a timestamp strtotime(date('Ym-d'))
then get the time saved in Database
then use this code.
$start_date = 'get the saved date in the database';
$current_date = strtotime(date('Y-m-d'));
$date1 = date_create(strftime('%Y-%m-%d',$start_date));
$date2 = date_create(strftime('%Y-%m-%d',$current_date));
$diff=date_diff($date1,$date2);
if($diff <= 360){
echo 'show the image';
}
else{
echo 'not show the image';
}
You can use momentjs to do that. For example displaying "1 min ago".
From the momentjs docs you can do this:
moment("20111031", "YYYYMMDD").fromNow(); // 7 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 6 years ago
moment().startOf('day').fromNow(); // 6 hours ago
moment().endOf('day').fromNow(); // in 18 hours
moment().startOf('hour').fromNow(); // 35 minutes ago
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.