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
#2
Pretty neat

I think
$msg1 and $msg2 should be $prefix and $suffix, but I don't see why either are even necessary when they are not inside the actual calculated values and can be in your content instead.

Also it would be easier to turn your timestamp into a valid date stamp and simply pass it all as 1 argument.

Then its a 1 argument function, or maybe pass in the date string and the format string as 2 arguments.

I would love to get togather a plugin for useful component functions, maybe incorporate it into shortcodes or something.

Stuff like this age function, an unlitmate excerpt function, etc.
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#3
shawn_a Wrote:Pretty neat

I think
$msg1 and $msg2 should be $prefix and $suffix, but I don't see why either are even necessary when they are not inside the actual calculated values and can be in your content instead.

Makes total sense - I'll change the value names soon. They are there mainly so that people can change the prefix/suffix for any purposes they have (to fit seamlessly to their content, to paste their own tags around it, to accomodate for their native language etc...). Also just for ease of use, so that when you simply call the function without any changes you know exactly what it does - otherwise you might be confused by just seeing the output "40 seconds" for example, thinking "what does that refer to exactly?"

shawn_a Wrote:Also it would be easier to turn your timestamp into a valid date stamp and simply pass it all as 1 argument.

Then its a 1 argument function, or maybe pass in the date string and the format string as 2 arguments.

A little confused since I'm not a very PHP person - do you mean just turning $timediff into:

Code:
$timediff = (time() - strtotime($date));

shawn_a Wrote:I would love to get togather a plugin for useful component functions, maybe incorporate it into shortcodes or something.

Stuff like this age function, an unlitmate excerpt function, etc.

That would be great if that could happen. What other functions are you thinking of (I'm a bit curious at this excerpt one)?
Reply




Users browsing this thread: 1 Guest(s)