This topic is locked
[SOLVED]

 Calculate Age

8/3/2019 10:51:33 AM
PHPRunner General questions
U
united001 author

I'm a rank amateur PHP user here ... using latest PHPRunner 10.2 ... All I am trying to do is to simply display Age as calculated from a table's "BirthDate" field using a code snippet inserted into either or both the Add or Edit on design section, nothing complex whatsoever. This very simple code snippet I found is from a very nice 2014 Xlinesoft Blog article:
$time1 = new DateTime('1971-03-10');

$time2 = new DateTime('today');

echo $time2->diff($time1)->y;
Used as is as a code snippet in my project design section works great as is, resulting in 48 printed out nicely on either page as it should.
I have tried repeatedly to replace DateTime('1971-03-10'); with DateTime ($data["BD"]) or some such where BD is the date field from my table in this case the value from the field is: 1952-4-17 but that does not work. What am I missing here? (Probably a lot!). How do I get the value from the BD field into DateTime() correctly for DateTime() to recognize it correctly?
Randy J.

M
Mark Kramer 8/3/2019



I'm a rank amateur PHP user here ... using latest PHPRunner 10.2 ... All I am trying to do is to simply display Age as calculated from a table's "BirthDate" field using a code snippet inserted into either or both the Add or Edit on design section, nothing complex whatsoever. This very simple code snippet I found is from a very nice 2014 Xlinesoft Blog article:
$time1 = new DateTime('1971-03-10');

$time2 = new DateTime('today');

echo $time2->diff($time1)->y;
Used as is as a code snippet in my project design section works great as is, resulting in 48 printed out nicely on either page as it should.
I have tried repeatedly to replace DateTime('1971-03-10'); with DateTime ($data["BD"]) or some such where BD is the date field from my table in this case the value from the field is: 1952-4-17 but that does not work. What am I missing here? (Probably a lot!). How do I get the value from the BD field into DateTime() correctly for DateTime() to recognize it correctly?
Randy J.



I would use javascript in the on OnLoad event of that page and calculate it there. See Calc on the fly See Here for Date Calc.. Look at those two for insperation.

Admin 8/3/2019

As a first step make sure you access underlying database data properly. Once you figured that you can perform your calculations.
For instance, there is no such thing as $data array in the code snippet. If you are on the Edit page and need to access record data check code examples at the end of this article:

https://xlinesoft.com/phprunner/docs/adding_php_code_snippet.htm. Example #2 is what you looking for.

U
united001 author 8/3/2019

Success! Thank you for the hint! I worked out the rest and learned the importance of global variables in the process. Thank you Mark, thank you Sergey! Now on to the next programming dungeon monster.
Randy J.

H
Hd_Mersintarim 8/4/2019

SELECT
DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(your date field, '%Y') AS age,
FROM your table