Welcome to Geeklog Sunday, July 21 2019 @ 09:22 pm EDT

Geeklog Forums

looking for a "time since" plugin


Status: offline

jetshack

Forum User
Full Member
Registered: 29/06/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

Status: offline

machinari

Forum User
Full Member
Registered: 22/03/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
PHP 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.
PHP 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.

Status: offline

jetshack

Forum User
Full Member
Registered: 29/06/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

PHP 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

Status: offline

machinari

Forum User
Full Member
Registered: 22/03/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.

Status: offline

jetshack

Forum User
Full Member
Registered: 29/06/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

PHP 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
PHP 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?


Status: offline

drshakagee

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

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


Yes I am mental.

Status: offline

jetshack

Forum User
Full Member
Registered: 29/06/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

Status: offline

machinari

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

 

Status: offline

machinari

Forum User
Full Member
Registered: 22/03/04
Posts: 1512
if you have trouble working out your date in seconds.. try this.
PHP 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.

Status: offline

jetshack

Forum User
Full Member
Registered: 29/06/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...

PHP 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

PHP 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

All times are EDT. The time is now 09:22 pm.

  • 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