Jump to content

Hanna Code; Date, calculate distance in years


kaz
 Share

Recommended Posts

Hi,

I would like to output the distance between a defined date and today in years. Example: 1980-07-01 (y-m-d) and today (current date, without time) calculate and echo via Hanna code.

<?php
$start = date('Y', strtotime($start));
$diff  = date('Y')-$start;
echo $diff;

Attributes:
start=1980

Page:
<p>Hannah Code Test: since [[count_years]] years</p>

count_years is the name of the Hanna Code. In my test, I tried it only with years, the result is:
since 0 years
so I had to put the month and day back for now. Code type is PHP code. I think the problem could be the PHP strtotime function?

Maybe someone has calculated the distance in years between a fixed date and the current date with Hanna code, and might be able to help?

Link to comment
Share on other sites

well if you want a dynamic code depending on an entry, yes you will 🙂
your hanna code tag could accept either [[count_years]] or [[count_year startyear="1920"]]

in this case you can just start your hannacode with a default value

$start = $startyear != '' ?  $startyear : 1980;

this way you cover both insertion, with and without attribute

and my pleasure 🙂

have a nice day

  • Like 2
Link to comment
Share on other sites

Btw, just subtracting years from each other may yield unexpected results if you’re thinking in terms of “birthdays”, as most people likely will. That is, considering your example of 1980-07-01, right now you would get 42. If that’s a person’s birth date we would think of them as 41 years old, though. 

Also, I realize your site is probably not in English, but as I understand it, this is one of the classic “false friends” where “since” feels like the German “seit”, but unlike “seit” it’s not used with time spans:

4 hours ago, kaz said:

<p>Hannah Code Test: since [[count_years]] years</p>

(I. e. you would only say “since 1980” or “since the relaunch”.)

Link to comment
Share on other sites

@Jan Romero
"since" I added only for understanding, it's not the original content. It's not about birthdays, it's for company anniversaries like this:
…, we have placed great value on personal advice for [[count_years]] years … therefore it is important that the full date Y-M-D added up the year.

Link to comment
Share on other sites

Hi,

@Jan Romero is right, if you only want to calculate the difference between two years, the first solution will work but, if you want to calculate the number of years between a specific date and today, then, as easy to achieve with php but switch to date_create() and date_diff()
 

<?php
$start = date_create('1980-07-01');
$today = date_create(date('Y-m-d'));
$diff = (array) date_diff($today, $start);

echo $diff['y'];

the result will change every 1rst of July, a simple way to calculate an age and not only the difference between two years
just print_r($diff) if you want to see how far you can go with what you get and could display with date_diff 🙂

have a nice day

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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