🌿 Budding noteworthy

A time_since function for PHP

planted on in: Programming, PHP, Revived and Wayback Machine.
~1,427 words, about a 8 min read.

I found it incredibly difficult to find any form of time_since function in #PHP so to save anyone else the trouble to hunting through hundreds of useless websites here is the function you may want to use, written by Natalie Downe[1] (you don't want to know how long it took to hunt it down). It takes the input in the form of the unix time stamp.

function time_since($original) {   
// array of time period chunks
$chunks = array(
array(60 * 60 * 24 * 365 , 'year' ),
array(60 * 60 * 24 * 30 , 'month' ),
array(60 * 60 * 24 * 7, 'week' ),
array(60 * 60 * 24 , 'day' ),
array(60 * 60 , 'hour' ),
array(60 , 'minute' ),
);

$today = time(); /* Current unix time */
$since = $today - $original;

// $j saves performing the count function each time around the loop
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];

// finding the biggest chunk (if the chunk fits, break)
if (($count = floor($since / $seconds)) != 0) {
// DEBUG print "<!-- It's $name -->n";
break;
}
}

$print = ($count == 1) ? '1 '.$name : "$count {$name}s";
if ($i + 1 < $j) {
// now getting the second item
$seconds2 = $chunks[$i + 1][0];
$name2 = $chunks[$i + 1][1];

// add second item if it's greater than 0
if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
$print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s";
}
}
return $print;
}

I also found a second more compact version from byteinsider[2] their version (below) takes the input in the form of the MySQL timestamp format (Y-m-d H:i:s) both functions output the same type of thing tho, so its up to you which you use.

function time_since($mysql_timestamp) {
$names = array('year','month','day','hour','minute','second');
$r = time()-strtotime($mysql_timestamp) - date('Z');
$posted = array(date('Y',$r)-1970,date('n',$r)-1,date('d',$r)-1, date('G',$r)-0,date('i',$r)-0,date('s',$r)-0);

for($n = 0; $n < 5 && $posted[$n] == 0; $n++);
$output = $posted[$n].' '.$names[$n];
if($posted[$n] != 1) $output .='s';
if($n < 5 && $posted[$n+1] != 0) {
$output .= ' '.$posted[$n+1].' '.$names[$n+1];
if($posted[$n+1] != 1) $output .='s';
}
return $output.' ago';
}

  1. In the many, many years since this article was written the original link for Natalie Downe's website has died. In this case the Wayback Machine has saved us by archiving the page for prosperity. ↩︎

  2. The domain for Byteinsider seems to have been lost to domain squatters however the Wayback Machine saves us once again. ↩︎


Page History

This page was first added to the repository on March 21, 2021 in commit 83e1321a and has since been amended 7 times. View the source on GitHub.

  1. refactor(#304): move files into src folder
  2. feat(#199): tag all posts that reference internet archive
  3. chore(#162): add hashtag to post for testing functionality
  4. feat(#108): removes categories in favour of tags (topics)
  5. feat(#108): moved content into digital garden structure and began work on content type pagination