kaz Posted January 27, 2022 Share Posted January 27, 2022 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 More sharing options...
virtualgadjo Posted January 27, 2022 Share Posted January 27, 2022 Hi, the issue is not an Hannacode one but a simple php one try $start = 1890; // your attribute no need of strtotime $thisyear = date('Y'); $diff = $thisyear - $start; echo $diff; it should work have a nice day 1 Link to comment Share on other sites More sharing options...
kaz Posted January 27, 2022 Author Share Posted January 27, 2022 Great, and I don't need the entry in the Attributes Panel. Thanks! Link to comment Share on other sites More sharing options...
virtualgadjo Posted January 27, 2022 Share Posted January 27, 2022 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 2 Link to comment Share on other sites More sharing options...
Jan Romero Posted January 27, 2022 Share Posted January 27, 2022 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 More sharing options...
kaz Posted January 27, 2022 Author Share Posted January 27, 2022 @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 More sharing options...
Jan Romero Posted January 27, 2022 Share Posted January 27, 2022 Yeah but still, count_years is always going to flip on New Year’s Day, so if you’ve been placing great value since December 31st, it’s going to say 1 year even though it’s barely been a day. Link to comment Share on other sites More sharing options...
ottogal Posted January 28, 2022 Share Posted January 28, 2022 Hi, wouldn't you want to calculate the $diff with the unformatted timestamps and just output the result in 'Y' Format? (Or I misunderstand the use case.) 1 Link to comment Share on other sites More sharing options...
virtualgadjo Posted January 28, 2022 Share Posted January 28, 2022 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 3 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now