Jump to content

Math using an event date on Calendar


Meddysong

Recommended Posts

Posted

My family is big and getting bigger, and I'm well beyond the point of being able to remember people's birthdays. That's not a problem with in-laws etc, but it won't go down very well with my niblings if the man masquerading as their favourite uncle forgets their birthday.

No problem: I've just created a calendar called Birthdays, set the events to recur yearly, and will have a widget of upcoming events from that calendar. Phew.

The next problem is that I don't necessarily know how old they are unless I do the maths. And considering I got my own brother a card with the wrong age on it once, there's scope for problems. (Not really my fault: He and I share a birthday. I used to buy his card whilst doing my newspaper route and deduct seven off my own age. Then one year I left home, had to buy the card in advance, deducted seven and ... yep.)

So, what I'd like help with, please, is how to code something that works out how old the person will be on the day of the event. I can use the HTML editor in the entry, so can create the date variable:

$date->setDate(1978, 12, 16);

And I presume that

echo $date->format('Y');

would give me 1978. Only I can't test it because the code is appearing within <p> tags once I click on source again. Maybe I'm doing something wrong.

Anyway, could somebody offer me some pointers, please, about how I could get the answer 1 to generate if I create an event on December 16th 2017 and indicate a $date of December 16th 2016? Something like

$date->setDate(1978, 12, 16);
$age->[some code to say the date of this event] - $date
echo $age->format('Y');

Thanks!

Posted

Try:

$date->setDate(1978, 12, 16);
$age = $date->diff( new \IPS\DateTime );
echo $age->y;

Or, if you want to compare against a specific date.

$birthday = new \IPS\DateTime;
$birthday->setDate( 1978, 12, 16 );
$day = new \IPS\DateTime;
$day->setDate( 2000, 01, 01 );
$ageAt2000 = $birthday->diff( $day );
echo $ageAt2000->y;

 

Posted
2 hours ago, Ryan Ashbrook said:

Try:


$date->setDate(1978, 12, 16);
$age = $date->diff( new \IPS\DateTime );
echo $age->y;

 

Thanks for coming by so quickly, Ryan. Unfortunately, I can't work around the central problem that what I type in as HTML after clicking on Source is being purified to

<p>
	$date-&gt;setDate(1978, 12, 16); $age = $date-&gt;diff( new \IPS\DateTime ); echo $age-&gt;y;
</p>

and so is just coming through as plain text after submission.

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...