Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XMA: "X Minutes Ago" Script
#1
A little script (which likely exists out there in multiple forms for other sites/systems/software, but I did this one from scratch to work with GetSimple) to output the difference in time between a supplied date and now.

Example: Have you got a blogging system set up on your GetSimple site? Have you ever thought it would be better for the date to show 'Posted X Days Ago'? Here is how.

Paste the below into a component or put it in your functions.php sheet for your template:

Code:
<?php
function get_time_ago($date, $prefix, $suffix, $secs, $mins, $hours, $days, $weeks, $years, $error) {
    $now = time();
    
    // default expressions for the message - outputs "Last edited X seconds/minutes/hours/days/weeks/years ago"
    if ($date == null) $date = returnPageField(return_page_slug(),'pubDate');
    if ($prefix == null) $prefix = 'Last edited';
    if ($suffix == null) $suffix = 'ago';
    if ($secs == null) $secs = 'seconds';
    if ($mins == null) $mins = 'minutes';
    if ($hours == null) $hours = 'hours';
    if ($days == null) $days = 'days';
    if ($weeks == null) $weeks = 'weeks';
    if ($years == null) $years = 'years';
    if ($error == null) $error = 'Invalid time stamp';
    
    // time difference
    $timestamp = strtotime($date);
    $timediff = ($now - $timestamp);

    // outputs the time difference in terms of seconds, minutes, hours, days, weeks or years
        if ((0 <= $timediff) && (($timediff) < 60)) { $timeago = round($timediff, 0); echo $prefix.' '.$timeago.' '.$secs.' '.$suffix; }
    elseif ((60 <= $timediff) && (($timediff) < 3600)) { ($timeago = round($timediff / 60, 0)); echo $prefix.' '.$timeago.' '.$mins.' '.$suffix; }
    elseif ((3600 <= $timediff) && (($timediff) < 86400)) { ($timeago = round($timediff / 3600, 0)); echo $prefix.' '.$timeago.' '.$hours.' '.$suffix; }
    elseif ((86400 <= $timediff) && (($timediff) < 604800)) { ($timeago = round($timediff / 86400, 0)); echo $prefix.' '.$timeago.' '.$days.' '.$suffix; }
    elseif ((604800 <= $timediff) && (($timediff) < 31449600)) { ($timeago = round($timediff / 604800, 0)); echo $prefix.' '.$timeago.' '.$weeks.' '.$suffix; }
    elseif (31449600 <= $timediff) { ($timeago = round($timediff / 31449600, 0)); echo $prefix.' '.$timeago.' '.$years.' '.$suffix; }
    else echo $error;
    }
    ?>

Then to call the function, use

Code:
<?php get_time_ago($date, $prefix, $suffix, $secs, $mins, $hours, $days, $weeks, $years); ?>

The default date that this checks the difference for is pubDate. You can change this date and the outputted message by putting your own values for $date, $prefix, etc...

For the blog example earlier, $prefix = 'Posted', $suffix = 'Ago', $days = 'Days' to produce the 'Posted X Days Ago' message.

---*edit*---

Changed $msg1 and $msg2 to $prefix and $suffix (@shawn_a)
Reply


Messages In This Thread
XMA: "X Minutes Ago" Script - by Angryboy - 2012-05-30, 23:25:58
XMA: "X Minutes Ago" Script - by shawn_a - 2012-05-31, 00:53:56
XMA: "X Minutes Ago" Script - by Angryboy - 2012-05-31, 01:24:08



Users browsing this thread: 3 Guest(s)