Saturday 12 Jul 2008

Age of an article in PHP

PHPI read a few days ago this article with the code to print the age of an article by Bleebot. As this function is long and not well optimized, I build my own one. ;-)


First some comments about the given function. Bleebot writes a lot of lines to rewrite existing functions. ;-)

$date2 = mktime(date("H"), date("i"), date("s"), date("m"),date("d"),date("Y"));

so $date2 is the actual time, it means simply the time() function!

list($year, $month, $day_time) = explode('-', $mysqlDate);
list($day, $time) = explode(" ", $day_time);
list($hour, $minute, $second) = explode(":", $time);
$date1 = mktime($hour, $minute, $second, $month, $day, $year);

All that to convert the date in timestamp. Which is the goal of the function strtotime. So an equivalent is $date1 = strtotime ($mysqlDate).


It's useless to caculate the summer or winter time, because we are in timestamp yet so a time in seconds since January 1, 1970. mktime has already do the job for summer or winter time to calculate this number of seconds!

$period = $timedifference;
$messtime = "";

2 useless lines! Why pass from $timedifference to $period?
Why put a empty string in $messtime, it's always initialized in the following? And simple quotes in strings when we don't use variables!

Then Bleebot takes care of the plural, 1 or several days/hours/minutes to write a 's' or not. This is a good idea, but when we must modify the text (for example for a translation), there are 6 places to modify. So why use memory to place it in a variable instead of writing it directly?


Of course, I don't say all that badly, it's just to make you learn some mistakes to not reproduce. :-)

Here is my solution:


function dateDiff ($date)
  $seconds = time() - strtotime ($date); /* The time difference in seconds. */

  /* We first find the unit: */
  if (($age = floor ($seconds /3600/24)) > 0)
    $unit = 'day';
  elseif (($age = floor ($seconds /3600)) > 0)
    $unit = 'hour';
    $age = floor ($seconds /60);
    $unit = 'minute';

  /* Then $plural = 's' if $age >1, else empty: */
  $plural = ($age > 1)? 's': '';

  echo "Posted $age $unit$plural ago.";

/* Call of the function: */
dateDiff ('2008-07-12 19:35:06');



Things to remember: mktime is useless here, it's generally used when we have pieces of date. In other cases, strtotime is a very powerful function which recognize the dates written on different formats, even in natural english: '2008-07-12', 'now', '12 July 2008', '+3 week 2 days 4 hours', 'next Thursday', 'last Monday'.

To optimize your code, only stock in variables what you really need to stock. Note that if you only want to change the name, the =& syntax allows to create an alias of a variable. For example to short $_SESSION in $s:

$s =& $_SESSION;

By modifying $s, it modifies $_SESSION, this it not a copy, but a kind of pointer.

If you have questions or remarks, just ask, I always answer to comments. :-)

cafĂ© Did this article help you? 
Buy me a coffee!

Leave a comment (all comments are moderated, don't waste time with spam)

Azure Dev