Skip to content

Instantly share code, notes, and snippets.

@FerFuego
Created October 8, 2020 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FerFuego/dcec26a8f75d7776b61538331a873d14 to your computer and use it in GitHub Desktop.
Save FerFuego/dcec26a8f75d7776b61538331a873d14 to your computer and use it in GitHub Desktop.
Calc time to posted
<?php
/**
* Return time to posted
*/
function time_to_post($post = null) {
$current_day = date('Y-m-d h:i');
$post_day = ( $post ) ? get_the_time('Y-m-d h:i', $post) : get_the_time('Y-m-d h:i');
$s_time = strtotime($current_day) - strtotime($post_day);
$m_time = intval($s_time/60);
$h_time = intval($s_time/3600);
$d_time = intval($s_time/86400);
if ($m_time < 1 ) {
$time = 'Few seconds ago';
} else {
if ($h_time < 1 ) {
$time = 'Few minuts ago';
} else {
if ($h_time == 1 ) {
$time = $h_time.' hour ago';
} else {
if ( $h_time > 24 ) {
$day = intval($h_time / 24);
if ( $day == 1 ) {
$time = $day . ' day ago';
} else {
if ( $d_time > 30 ) {
$mount = intval($d_time / 30);
if ( $mount == 1 ) {
$time = $mount . ' month ago';
} else {
$time = $mount . ' months ago';
}
} else {
$time = $d_time.' days ago';
}
}
} else {
$time = $h_time.' hours ago';
}
}
}
}
return $time;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment