Search code examples
phpexpressionengine

If current_time then do this else do this


I have an expressionengine site and I want to create a news template that populates with only one year of stories. The following code does work and populate the page with the correct year of stories.

<?$date = strtotime('{current_time format="%Y"} -1 year');?>
    {exp:channel:entries channel="news" start_on="<?echo date('Y', $date);?>-12-31 20:00" dynamic="no"}

BUT in January and Feburary I would not have many posts on the page so I want to something like:

{if '{current_time format="%M"} = "Jan"'}
<?$date = strtotime('{current_time format="%Y"} -2 year');?>
{exp:channel:entries channel="news" start_on="<?echo date('Y', $date);?>-12-31 20:00" dynamic="no"}
{if:else}
<?$date = strtotime('{current_time format="%Y"} -1 year');?>
    {exp:channel:entries channel="news" start_on="<?echo date('Y', $date);?>-12-31 20:00" dynamic="no"}
{/if}

I can't seem to figure out the PHP for the IF current time = Jan


Solution

  • According to the documentation found here https://ellislab.com/expressionengine/user-guide/templates/date_variable_formatting.html

    You should use

    {if {current_time format="%M"} == "Jan"}