Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: time ago for articles  (Read 287 times)

adminphp

  • Full Member
  • ***
  • Karma: 4
  • Posts: 150
  • you must be a great snewser .
time ago for articles
« on: October 03, 2012, 04:27:35 AM »

Hello Again Friends. how do you DO? I hope fine.

version 1.6 has a date in the infoline when the articles published.
but this date is so simple (01.10.2012. 21:21) if i can say. so i thought in other techniques which can be used for (comments if it's possible) and articles time ago. so can we have a php function do this work.
for instance i want time ago above titles (It’s been “10 minutes, 28 seconds” since posted) and when the publish time passed one day (Means  24 hours) then the form of date will be like this; (Monday 11 June 2012).
do you have any idea about this? thanks alot friends. ;)
« Last Edit: October 03, 2012, 07:57:13 AM by adminphp »
Logged
“Fifty years of programming language research, and we end up with C++?”

Fred K

  • Still trying to learn stuff
  • ULTIMATE member
  • ******
  • Karma: 130
  • Posts: 2728
    • Personal
Re: time ago for articles
« Reply #1 on: October 03, 2012, 04:50:42 PM »

Hi. Have you searched for a solution? Because there's a mod by mdj here and I made a slight variation on it with the help of nukpana which is available here. I made my variation only because I was too lazy to search the forum so I didn't know Matt had already posted a mod... so, search is your friend.

Matt's mod was done for 1.7 and for comments but the date function is the same for articles and I believe it is the same in 1.6 so it should reasonably be easy to implement in the article scenario - although I haven't tested it.
« Last Edit: October 03, 2012, 04:58:15 PM by Fred K »
Logged

adminphp

  • Full Member
  • ***
  • Karma: 4
  • Posts: 150
  • you must be a great snewser .
Re: time ago for articles
« Reply #2 on: October 04, 2012, 12:18:52 AM »

i've tested the function and honestly it doesn't work with me (V1.6) and the place where suppose the time shows it displays: " No date Provided" and just to give an idea, I'm making this function for article not for comments. and after I checked the comments about thread. there is people says this mod didn't work with them.
Logged
“Fifty years of programming language research, and we end up with C++?”

Fred K

  • Still trying to learn stuff
  • ULTIMATE member
  • ******
  • Karma: 130
  • Posts: 2728
    • Personal
Re: time ago for articles
« Reply #3 on: October 04, 2012, 01:56:14 AM »

Well I just now did a quick test with my version in 1.7 and it works without a problem. I don't have 1.6 installed anywhere so I can't test it right now but unless something's drastically changed since I last looked at 1.6 I don't really see how it would be any different.

This is untested in 1.6. In snews.php, find the two blocks which deals with the article infoline. Before the first block, which should begin with something like "if(!empty($currentPage)) {", you place the following code:

Code: [Select]
// NICE TIME
function nicetime($date) {
    if(empty($date)) {
        return "No date provided";
    }
   
    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths = array("60","60","24","7","4.35","12","10");
   
    $now = time();
    $unix_date = strtotime($date);
   
       // check validity of date
    if(empty($unix_date)) {   
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {   
        $difference     = $now - $unix_date;
        $tense         = "ago";
       
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
   
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
   
    $difference = round($difference);
   
    if($difference != 1) {
        $periods[$j].= "s";
    }
   
    return "$difference $periods[$j] {$tense}";
}

$date = $a_date_format; // Uses date format set in db for articles
$result = nicetime($date); // Shows time ago

And then in the two if($infoline == true) parts, in case 'date', instead of the equivalent of this line:
Code: [Select]
echo $a_date_format;
you put this line:
Code: [Select]
echo nicetime($date);
As I said before, to get the exact functionality you are after (the actual publishing date instead of "x z's ago" when older than 24 hours) you'll need to modify parts of the nicetime function. I can't help with that right now because I haven't figured out how ... yet (haven't even given it much thought to be honest) however the nicetime function itself should work just fine in 1.6 as well as 1.7. In 1.7 it works, that much I know.
Logged