Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
Meddysong Posted September 28, 2017 Posted September 28, 2017 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!
Ryan Ashbrook Posted September 28, 2017 Posted September 28, 2017 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;
Meddysong Posted September 28, 2017 Author Posted September 28, 2017 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->setDate(1978, 12, 16); $age = $date->diff( new \IPS\DateTime ); echo $age->y; </p> and so is just coming through as plain text after submission.
Ryan Ashbrook Posted September 28, 2017 Posted September 28, 2017 Oh, yes, this will not work when using an Editor field - so it won't work in forum posts, blog entries, etc, unfortunately.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.