Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Conditional with just part of a date

Go to End

3 Posts   1410 Views


Community Member, 21 Posts

3 July 2010 at 8:24am

In my db I have a series of projects, each with a StartDate and EndDate.

So, for example...

Name: Project 1; StartDate: 1/1/2009; EndDate: 12/31/2009
Name: Project 2; StartDate: 7/1/2007; EndDate: 6/30/2012

When displaying these projects, I only want to display the year AND if the start and end dates are in the same year, I would only like to show the year once (i.e., 2009 rather than 2009-2009)

So, for the above example, I'd like the output to be...

Project 1 (2009)

Project 2 (2007-2012)

How can I compare just the years from the date fields to determine whether to output (StartDate.Year) or (StartDate.Year-EndDate.Year) in my template?

Thanks so much!



Community Member, 712 Posts

7 July 2010 at 4:01pm

Rather than trying to do this in your template, add a new function to your Project object that creates the string for you.


function Years() {
  $start = $this->dbObject('StartDate')->Year();
  $end = $this->dbObject('EndDate')->Year();
  return ($start == $end) ? $start : "{$start} - {$end}";

Now in your template you can just do:

$Project.Name ($Project.Years)


Community Member, 21 Posts

8 July 2010 at 2:03am

Thanks Hamish!

I had tried something similar, but didn't have the syntax quite right. Your code worked perfectly. Thanks again.