using operators to check how recent a post is
Posted: 17 May 2006 09:37 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  49
Joined  08-25-2005

I’m trying to do check how recent a post is and use that information in a conditional. There is the suggestion that this should be possible in this thread but I cannot get it to work. I’ve tried using just the ee date fields—{entry_date} and {current_time} and also have tried with php, and I can display both the posted time and the current time without a probllem but cannot check the difference between them.

The idea is to only show the posted date for those entries that are less than x hours old (in the example below, 4 hours, or 14400 seconds).

Most recent attempt was this:

{exp:weblog:entries weblog="{master_weblog_name}" category="22" orderby="date" sort="desc" limit="{qntoshow}" entry_id="not {right01}|{right02}|{right03}|{right04}"}
<a class="noline" href="/index.php/igb/listqn#{entry_id}"><h2 class="quicknewstitle2">{title}</h2></a>
<
div class="posted3">
Raw Entry Date = {entry_date}<br />
Raw Today's Date = {current_time format="%U"}<br />
{if {current_time} - {entry_date} < "14400" }
Posted at {entry_date format='
%h:%i %A'}<br />
{/if}
</div>
{/exp:weblog:entries}

What I get is this:

Raw Entry Date = 1147821780
Raw Today’s Date = 1147880063

but the {if} clause never generates anything, regardless of what I do with the number of seconds, make it > or < or whatever.

Any suggestions would be most welcome.

Thanks.

Profile
 
 
Posted: 17 May 2006 10:21 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23485
Joined  05-20-2002

I believe you’d want to do it via php- I don’t believe your math is getting done that way- and it’s probably not an if syntax that’s getting recognized.  So- php parsing on output:

<?php
$mytime
= {current_time} - {entry_date};
if (
$mytime < 14400)
{
?>
Posted at {entry_date format
='%h:%i %A'}<br />
<?php
}
?>

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 17 May 2006 12:45 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  49
Joined  08-25-2005

EDIT: php is enabled and set to output.

Okay, I’ve pasted the code below in its own template, which you can see here.

I think the problem I’m running into is with the {current_time} variable. As you can see by looking at the link above, simply using {current_time} doesn’t produce anything. If I add in the formatting directions I get the unix timestamp.

However, if I use the {current_time format=”%U”} version in the PHP statement I get a blank screen. Without the formatting (in the PHP statement) {current_time} appears to evaluate to zero; hence all my $mytime variables are equal to the “0 - {entry_date}”.

{exp:weblog:entries weblog=“IGB” category=“22” orderby=“date” sort=“desc” limit=“5” }
<a href=”/index.php/igb/listqn#{entry_id}”>{title}</a><br />
Entry date = {entry_date} <br />
Current time (no formatting) = {current_time} <br />
Current time (with formatting) = {current_time format=”%U”} <br />
<?php
$mytime = “{current_time}” - “{entry_date}”;
echo “The variable mytime equals: ” ;
echo $mytime;
if ($mytime > 14400)
{
?>
Posted at {entry_date format=’%h:%i %A’}<br />
<?php
}
?>
<br />
<br />
{/exp:weblog:entries}

Profile
 
 
Posted: 17 May 2006 01:07 PM   [ Ignore ]   [ # 3 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23485
Joined  05-20-2002

Hm- I THINK the entry date is parsed according to localized time at that point- so try:

<?php
global $LOC;
$mytime = $LOC->set_localized_time() - "{entry_date}";
echo
"The variable mytime equals: " ;
echo
$mytime;
if (
$mytime > 14400)
{
?>
Posted at {entry_date format
=%h:%i %A’}<br />
<?php
}
?>

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 17 May 2006 01:24 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  49
Joined  08-25-2005

Very nice. The template now looks just as it should, using your suggestion thusly:

{exp:weblog:entries weblog=“IGB” category=“22” orderby=“date” sort=“desc” limit=“5” }
<a href=”/index.php/igb/listqn#{entry_id}”>{title}</a><br />
Entry date = {entry_date} <br />
Current time =
<?php
global $LOC;
$mytime = $LOC->set_localized_time();
echo $mytime; ?>
<br />
Current time (with formatting) = {current_time format=”%U”} <br />
<?php
global $LOC;
$mytime = $LOC->set_localized_time() - “{entry_date}”;
echo “The variable mytime equals: ” ;
echo $mytime;
if ($mytime > 14400)
{
?>
<br />
Posted at {entry_date format=”%h:%i %A”}<br />
<?php
}
?>
<br />
<br />
{/exp:weblog:entries}

Next I’m going to see if I can get it to work in the actual page template (where there may be a complication related to some other PHP that I believe may have to be parsed on input)

Thanks very much for the quick response and solution.

Profile
 
 
Posted: 17 May 2006 03:59 PM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  49
Joined  08-25-2005

Edited to make a little more sense


Okay, I did in fact run into an issue, as I am loading some variables into the template with a php include and that requires parsing on input. So, taking an entirely different approach, I found the tag definition for {relative_date} in mod.weblog.php, at about line 2972.

Then defined a new tag called {elapsed_seconds} by adding in the following:

          //————————————————————
          //  {elapsed_seconds} added by harrelson 5/17/2006
          //————————————————————
         
          if ($key == “elapsed_seconds”)
          {
          $tagdata = $TMPL->swap_var_single($val, $LOC->now - $row[‘entry_date’], $tagdata);
          }

 

This is just the {relative_date} modified to take out the function that converts into the Years, Months, Weeks, Hours, Seconds format that I was unable to check in a conditional.

Then in my template I add:

{if {elapsed_seconds} < 14400}
<div class=“posted3”>Posted at {entry_date format=’%h:%i %A’}
<br /></div>
{/if}

and it seems to work fine.

The downside, of course, is having somebody like me mucking around in the code. I’ll try to read up on plugins and see if there’s a more organized way to do this.

But it works.

Thanks for the help.

Profile
 
 
Posted: 18 May 2006 06:35 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Rank
Total Posts:  49
Joined  08-25-2005

Well, I thought I was on to something, but I don’t understand the way it’s behaving. My newly minted {elapsed_seconds} tag works fine in a conditional, but only if you have first called that variable outside of the conditional. The test page is here.

The complete template is:

With the elapsed_seconds tag by itself <br />
{exp:weblog:entries weblog="IGB" category="22" orderby="date" sort="desc" limit="5" }
<a href="/index.php/igb/listqn#{entry_id}">{title}</a><br />
{elapsed_seconds} <br />
{if {elapsed_seconds} < 100000}
<div class="posted3">Posted at {entry_date format='%h:%i %A'}
<br /></div>
{/if}{/exp:weblog:entries}
<br />
<
hr>
Without the elapsed_seconds tag by itself <br />
{exp:weblog:entries weblog="IGB" category="22" orderby="date" sort="desc" limit="5" }
<a href="/index.php/igb/listqn#{entry_id}">{title}</a><br />
{if {elapsed_seconds} < 100000}
<div class="posted3">Posted at {entry_date format='%h:%i %A'}
<br /></div>
{/if}{/exp:weblog:entries}

In the first of the two web entries tags above, note the fourth line, which just outputs the value of {elapsed_seconds}. As long as I do that, the conditional is fine. In the second of the web entries tags, that line is omitted, and the conditional doesn’t work.

Another note—I have to have the elapsed_seconds tag enclosed in braces ({elapsed_seconds}) for the conditional to work.

thanks for any light you can shed on this.

Profile
 
 
Posted: 18 May 2006 07:29 AM   [ Ignore ]   [ # 7 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23485
Joined  05-20-2002

I won’t swear on this one- but look around line 2275:

// ----------------------------------------
    //   Conditionals
    // ----------------------------------------

Try adding it in there as part of the $cond array- see if that makes any difference.

ETA- I also don’t think this would be a very hard plugin to write- which would save hassles come upgrade time.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 18 May 2006 07:47 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Rank
Total Posts:  49
Joined  08-25-2005

I took a quick stab at adding into the conditional thing and brought everything down—so I backed out of there fairly quickly.  I’ll try it on a test site and let you know.

On the plugin, I’ll take a stab at that, but is there reason to think that doing this as a plugin would address the need to explicitly call the variable outside of the conditional in order for the conditional to work?

Profile
 
 
Posted: 18 May 2006 09:32 AM   [ Ignore ]   [ # 9 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23485
Joined  05-20-2002

Yea- I’m not sure what’s up with having to call it first.  Doesn’t make sense to me off the top of my head.  But- did a quick plugin, seems to work:

<?php

class Date_compare
{

var $return_data = "";

  function
Date_compare()
  
{
    
global $TMPL, $LOC;
    
$d_compare = $TMPL->fetch_param('edate');
    
$d_range = ($TMPL->fetch_param('range')) ? $TMPL->fetch_param('range') : 86400; //defaults to one day, in seconds
    
$time_interval = $LOC->set_localized_time() - $d_compare;
    if (
$time_interval < $d_range)
    
{  
        $this
->return_data = $TMPL->tagdata;
      
}
    
else
    
{
        $this
->return_data = "";

    
}
  }
}
?>


Call it pi.date_compare.php and put it in your plugin folder.  Then try:

{exp:weblog:entries weblog="IGB" category="22" orderby="date" sort="desc" limit="5" }

{exp
:date_compare edate="{entry_date}" range="86400"}
<b>Published in last day!</b>
{/exp:date_compare}

{
/exp:weblog:entries}

Seemed to work in a really quick test.  Should be enough to get you going.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 18 May 2006 10:14 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Rank
Total Posts:  49
Joined  08-25-2005

Ah, I’ll have to save my first plugin attempt for another day. This is exactly what I’ve been looking for. Thanks so much. Seems to work beautifully. I managed (after several failed attempts) to get the name/author/other stuff inserted so it shows up in the plugin manager.

Thanks again.

Profile
 
 
Posted: 20 May 2006 02:46 AM   [ Ignore ]   [ # 11 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  15167
Joined  05-15-2004

Wiki’d.

 Signature 

Everything will be good in the end. If it’s not good, it’s not the end.

Profile
MSG
 
 
Posted: 06 February 2007 03:42 PM   [ Ignore ]   [ # 12 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  226
Joined  03-06-2003

Robin, thanks for posting this plugin, I tried it out according to the instructions and wasn’t able to get it to work.  I made a template like the one you posted as an example, which opens as a blank page (no page title) in the browser.  The page prints properly when I take the plugin-specific tag pair out.

What would be the most likely cause of the problem (aside from my technical ineptitude)?

Profile
 
 
Posted: 06 February 2007 03:51 PM   [ Ignore ]   [ # 13 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23485
Joined  05-20-2002

Did you create the plugin file using the above?  Could be an issue there- sounds like you may be throwing a php error.  If you want to attach the file you made, I can take a look at it.  See if I spot anything.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 06 February 2007 04:21 PM   [ Ignore ]   [ # 14 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  226
Joined  03-06-2003

Thanks for the quick reply, Robin.  I did create a plugin file using the above example you posted.  Is there much more to it than creating a txt file, pasting in your example code, renaming it as you indicated, and saving it into the plugins directory in ASCII mode?  It seems pretty straightforward.  Is there anything special I have to do to the template I’m using (which also follows your example, but with my own blog info popped in there)?

Profile
 
 
Posted: 06 February 2007 05:01 PM   [ Ignore ]   [ # 15 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  23485
Joined  05-20-2002

No telling what’s up- it was a quicky plugin, and may have issues with an embed as far as parse order?  Or not.  I made a quicky version, tested on mine- it’s attached.  Download, open, upload to system/plugins- replace the other one if it’s there.  Then just to make sure it works- try this code in a test template:

{exp:weblog:entries}
{title}
- {entry_date format="%Y %m %d"}
{exp
:date_compare edate="{entry_date}" range="86400"}
<b>Published in last day!</b>
{/exp:date_compare}
<br>
{/exp:weblog:entries}


Should work ok- and I did try it with an embed and it worked.  That said?  I really wouldn’t nest an embed inside a weblog tag- I’d find a different way to go at whatever you’re doing.  That could get a bit resource intensive.

File Attachments
pi.date_compare.zip  (File Size: 1KB - Downloads: 210)
 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64529 Total Logged-in Users: 29
Total Topics: 81096 Total Anonymous Users: 23
Total Replies: 436396 Total Guests: 243
Total Posts: 517492    
Members ( View Memberlist )