Welcome to Geeklog, Anonymous Thursday, March 28 2024 @ 06:39 am EDT

Geeklog Forums

looking for a "time since" plugin


Status: offline

jetshack

Forum User
Full Member
Registered: 06/29/04
Posts: 122
Location:Texas
grumpy
I'm fairly new to GeekLog, having switched from Wordpress and am still in the process of figuring out all of the bells and whistles.

Anyway, I'm looking for a "time since" plugin for my site to count the days since I gave up my 18 year addiction to Copenhagen (currently at 9 days). I would think that this would be similar to the countdown plugin.

If none currently exists then I'm up for trying to make it myself, but that could cause me to relapse since I'm oh so very bad at PHP!.

Thanks
Jacks
JetShack
 Quote

Status: Banned

machinari

Forum User
Full Member
Registered: 03/22/04
Posts: 1512
a good place to start in your PHP effort is in lib-custom.php. that's where you will place your function. then you only need to create a block to contain the output.

found this interesting script at this page
Text Formatted Code
<?php

/* Works out the time since the entry post, takes a an argument in unix time (seconds) */
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;
}

?>

 
found another one at this page.
Text Formatted Code
<?
function elapsed($year, $month, $day, $hour, $minute) {

  // No copyright, use and fiddle with as you see fit, just don't steal credit for it,
  // as it is based on the work of myself and several other people.
  // Call the function with the command elapsed(yyyy,mm,dd,hh,mm), for example:
  // elapsed(1974,5,14,06,10) will produce the elapsed time since my birth date.
 
  // This script will automatically omit parts that return a zero value, so that 1 year,
  // 3 months, 0 days, 0 hours and 0 minutes will not show the days, hours and minutes,
  // as that would be quite pointless and ugly.
 
  $todayMonth = date(n);
  $todayDay = date(j);
  $todayHour = date(G);
  $todayMinute = date(i);
  $monthday = date(nj);
  $link = $month.$day;
  $datepoint = $link;

  if ($monthday < $datepoint) $years = date(Y) - $year - 1;
  else $years = date(Y) - $year;
  if ($years < 1) $yearWord = '';
  elseif ($years == 1) $yearWord = 'year,';
  elseif ($years > 1) $yearWord = 'years,';
  if ($todayMonth > $month) {
    if ($todayDay < $day) $months = $todayMonth - $month -1;
    else $months = $todayMonth - $month;
  } else {
    $between = $month - $todayMonth;
    $plus = 12 - $month;
    if ($todayDay < $day) $months = $month + $plus - $between -1;
    else $months = $month + $plus - $between;
  }
  if ($months == 1) $monthWord = 'month,';
  elseif ($months > 1) $monthWord = 'months,';
  if ($todayDay > $day) {
    if ($todayHour < $hour) $days = $todayDay - $day -1;
    else $days = $todayDay - $day;
  } else {
    $between = $day - $todayDay;
    $daysToEndOfMonth = date(t) - $day;
    if (date(t) == 28) $plus = $daysToEndOfMonth;
    if (date(t) == 29) $plus = $daysToEndOfMonth;
    if (date(t) == 30) $plus = $daysToEndOfMonth;
    if (date(t) == 31) $plus = $daysToEndOfMonth;
    if ($todayHour < $hour) $days = $day + $plus - $between - 1;
    else $days = $day + $plus - $between;
  }
  if ($days == 1) $dayWord = 'day,';
  else $dayWord = 'days,';
  if ($todayHour > $hour) {
    if ($todayMinute < $minute) $hours = $todayHour - $hour - 1;
    else $hours = $todayHour - $hour;
  } else {     $between = $hour - $todayHour;
    $plus = 24 - $hour;
    if ($todayMinute < $minute) $hours = $hour + $plus - $between - 1;
    else $hours = $hour + $plus - $between;
  }
  if ($hours == 1) $hourWord = 'hour,';
  if ($hours > 1) $hourWord = 'hours,';
  if ($todayMinute > $minute) {
    $minutes = $todayMinute - $minute;
  } else {     $between = $minute - $todayMinute;
    $plus = 60 - $minute;
    $minutes = $minute + $plus - $between;
  }
  if ($minutes == 60) $minutes = 0;
  if ($minutes == 1) $minuteWord = 'minute';
  elseif ($minutes > 1) $minuteWord = 'minutes';
  if ($years == 0) { $years = ''; $yearWord = ''; }
  if ($months == 0) { $months = ''; $monthWord = ''; }
  if ($days == 0) { $days = ''; $dayWord = ''; }
  if ($hours == 0) { $hours = ''; $hourWord = ''; }
  if ($minutes == 0) { $minutes = ''; $minuteWord = ''; }
  echo $years.' '.$yearWord.' '.$months.'  '.$monthWord.' '.$days.' '.$dayWord.' '.$hours.' '.$hourWord.' '.$minutes.' '.$minuteWord.'';
}
?>


 
lots of these around the net. hope that helps.
 Quote

Status: offline

jetshack

Forum User
Full Member
Registered: 06/29/04
Posts: 122
Location:Texas
allright, I figured it wouldn't be easy but I'm already stumped.

I added the following to my lib-custom.php

Text Formatted Code

/**
* timesince PHP Block function
*
* Copenhagen free
*/
function phpblock_timesince()
{
        $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 then created a PHP block
Titled = Copenhagen free
Block name = timesince
Block type = php
Block Function = phpblock_timesince

after saving the block the following was returned

Error in PHP Block. Function, phpblock_timesince, does not exist.
I'm not naming the block correctly? I've looked at this for several minutes and can't figure out what's wrong... I know that it won't work, but this isn't the error I was expecting.

Any help?

Later,
Jacks
JetShack
 Quote

Status: Banned

machinari

Forum User
Full Member
Registered: 03/22/04
Posts: 1512

did you resave your lib-custom.php properly? and to the proper path?

don't forget about the parameter to be passed to the function--a unix date in seconds.
 Quote

Status: offline

jetshack

Forum User
Full Member
Registered: 06/29/04
Posts: 122
Location:Texas
great! that was a bonehead move... I'd put the lib-custom file in the root.
now it's working great.

my last question on this, at the tail end of the code

Text Formatted Code
        }

    }
       
        return $print;
       
}
 


I want to customize the output by saying something like "Jon's been Copenhagen free for xxxxx"

I've tried to print this text with
Text Formatted Code
        }

    }
        print("Jon's been Copenhagen free for ");
        return $print;
 


but its not showing up. I've also tried echoing it but same thing. What am I missing?

 Quote

Status: offline

drshakagee

Forum User
Full Member
Registered: 10/01/03
Posts: 231
Try something like this:
Text Formatted Code
}

    }
        $print .= 'Jon's been Copenhagen free for ';
        return $print;

 

Yes I am mental.
 Quote

Status: offline

jetshack

Forum User
Full Member
Registered: 06/29/04
Posts: 122
Location:Texas
tried the above, this is the error

Fatal error: Call to undefined function: 1 week, 2 days() in /home/jon/public_html/system/lib-custom.php on line 241
 Quote

Status: Banned

machinari

Forum User
Full Member
Registered: 03/22/04
Posts: 1512
works for me...
did you pass an original date to the function?
like this:
Text Formatted Code
function phpblock_timesince() {
    $original = '483627987';//some date in unix time.  (seconds)

 
 Quote

Status: Banned

machinari

Forum User
Full Member
Registered: 03/22/04
Posts: 1512
if you have trouble working out your date in seconds.. try this.
Text Formatted Code
function phpblock_timesince() {
    $original = mktime(0,0,0,5,17,2002);// mktime(hour,min,sec,month,day,year)

 

this one tells me how old my son is. Laughing
 Quote

Status: offline

jetshack

Forum User
Full Member
Registered: 06/29/04
Posts: 122
Location:Texas
yeah I forgot to mention that I'd put the unixtime in, my problem was with it printing out extra text in addition to the time... I figured it out though...

Text Formatted Code
/**

* timesince PHP Block function

*

* Copenhagen free

*/

function phpblock_timesince()

{

        $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'),

    );

    $original = mktime(1,1,1,1,2,2005);
   
    $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;

        }

    }




    if ($i + 1 < $j) {

        // now getting the second item

        $seconds2 = $chunks[$i + 1][0];

        $name2 = $chunks[$i + 1][1];


    $print = 'Jon has been Copenhagen free for ';

    $print .= ($count == 1) ? '1 '.$name : "$count {$name}s";


        // 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;
       
}



 


The big change being

Text Formatted Code
    $print = 'Jon has been Copenhagen free for ';

    $print .= ($count == 1) ? '1 '.$name : "$count {$name}s";



 


It's not displaying the hrs/mins/secs/ but I'm ok with that. All in all I learned a little bit and am happy with the results. Thanks for your help guys... Finished just as my patch was wearing off! you can see it here

Later,
Jacks
 Quote

All times are EDT. The time is now 06:39 am.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content