ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Get last year’s date

February 16, 2011 2:11pm

Subscribe [5]
  • #1 / Feb 16, 2011 2:11pm

    PhireGuys

    525 posts

    Is there a way to get last year’s date?  Also, what about last month, or yesterday?  I know there is {current_time} but I want it based on current_time but last year, or month or anything.

    Is there a plugin or built-in way to do this?

  • #2 / Feb 16, 2011 2:41pm

    Ingmar

    29245 posts

    No, you’d have to use some PHP, I am afraid. Not too difficult, but nothing built in.

  • #3 / Feb 16, 2011 2:43pm

    PhireGuys

    525 posts

    Thanks for the quick reply.  I was afraid I’d have to use PHP but since we are using it for other things (and input/output modes will be important) I was hoping to be able to do it with EE.

    Thanks though.

  • #4 / Feb 17, 2011 3:04am

    John Henry Donovan

    12339 posts

    There could be an add-on here that might do it for you

  • #5 / Feb 18, 2011 11:19am

    PhireGuys

    525 posts

    Thanks, I looked through but found nothing :(.

    Just gotta do it with PHP it seems.

  • #6 / Feb 18, 2011 3:32pm

    Ingmar

    29245 posts

    Yes, but that shouldn’t be too much of a problem. Moving to the CSC.

  • #7 / Feb 18, 2011 3:47pm

    Mark Bowen

    12637 posts

    Best way I can think of doing this would be to create a plugin that uses the strtotime() PHP function. This way you can then pass in certain relative time formats and output what you need.

    Just wondering out of interest as to why you need this though?

    Best wishes,

    Mark

  • #8 / Feb 18, 2011 4:07pm

    PhireGuys

    525 posts

    I needed to display last year’s date on a page and also pull in entries only from last year.  They have awards that they present at the beginning of the year for the previous year.  So we created a page that dynamically shows those awards (channel entries) but we had to make sure we are always pulling last years awards on the main page.

    I ended up doing this:

    <?php  date("Y")-1; ?>

    I’m using that to display last year’s Year and also for the year=”” parameter in the channels:entries tag.

    But, yeah, a plugin would be much better so I don’t have to worry about parsing with PHP.

  • #9 / May 27, 2012 12:58am

    kpeacock

    2 posts

    Hey PhireGuys (or anyone else who can help).  I’m in the same situation, where I want to show AGM documents from previous years.  I’ve tried this code, based on what you posted above, but the php in the year=”” parameter doesn’t seem to work. 

    The first chunk of code below is meant to display the documents for the current year, while the second is meant to display last year’s.  It displays the correct title for last year using the PHP code, but doesn’t seem to work for the year=”” parameter, so I end up with the current year’s documents under the last year header.

    <h2>{current_time format="%Y"}</h2>
    <p>{exp:channel:entries channel="casea_documents" category="49" orderby="casea_docs_date" sort="desc" year="{current_time format="%Y"}"}<br />
    <a href="http://{casea_docs_file}" target="_blank" rel="noopener">{title}</a></br><br />
    {/exp:channel:entries}</p>
    
    <h2><?php echo date("Y")-1; ?></h2>
    <p>{exp:channel:entries channel="casea_documents" category="49" orderby="casea_docs_date" sort="desc" year="<?php date("Y")-1; ?>"}<br />
    <a href="http://{casea_docs_file}" target="_blank" rel="noopener">{title}</a></br><br />
    {/exp:channel:entries}

    Returns:

    2012

    2012 CASEA AGM Agenda
    2012 CASEA President’s Report
    2012 CASEA Vice President’s Report
    2012 CASEA Grad Student Report
    2012 CASEA Treasurer’s Report

    2011

    2012 CASEA AGM Agenda
    2012 CASEA President’s Report
    2012 CASEA Vice President’s Report
    2012 CASEA Grad Student Report
    2012 CASEA Treasurer’s Report

    Anyone know if this is possible or if I’ve just got something wrong?

    Thanks!

  • #10 / May 27, 2012 11:18am

    John St-Amand

    865 posts

    kpeacock - in your case, if the entries were entered int the year in question, then the date heading variable pair would work for you.  Since your headings and groupings are based on the “real” date and not “last year” as a calculation relative to current time.  You would just using the date heading to brake up the entries in the channel.  No need for a PHP calculation, I wouldn’t think.

    Hope that helps you.

  • #11 / May 27, 2012 11:21am

    John St-Amand

    865 posts

    I suspect that if you did need to look at last year, last month, etc. then if you’re not comfortable with PHP, you can find a way to do the math using one of the various simple math add-ons available at devot-ee.com.  To get last year you could just subtract one from {current_time format="%Y"} for example and use the resulting calculated value as a parameter.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases