years of age, months and days

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
lramirez
Veteran Member
Posts: 46
Joined: 2019-11-01 23:23

years of age, months and days

Post by lramirez » 2020-02-16 17:42

I'm a beginner programming

// calculate age in years, months and days

// I put the function: (In the hook folder, file "unit")

function unit_init (& $ options, $ memberInfo, & $ args) {

if (isset ($ _ REQUEST ['SelectedID'])) {
$ id = makeSafe ($ _ REQUEST ['SelectedID']);
$ today = date ('Y-m-d');

sql ("update unit set age = floor (datediff ('{$ today}', date) / 365.25) where id = '{$ id}'", $ eo);
}

// show age = correct
// but as I show months and days :D
Luis Ramirez R.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: years of age, months and days

Post by pbottcher » 2020-02-16 18:39

Hi,

can you explain what exactly you are looking for. From the sql statement I would assume that you set the age to amount of years of the diff between today and date (where date is an unfortune name for that field).
Also you could use now() in the sql statement directly instead of getting the current date into the today variable.

Maybe you can also use TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2) with unit = YEAR, datetime_expr1 = date and datetime_expr2=NOW()
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

lramirez
Veteran Member
Posts: 46
Joined: 2019-11-01 23:23

Re: years of age, months and days

Post by lramirez » 2020-02-19 19:06

good morning ... excuse me

I speak Spanish and I am using the translator.
I want to see the years of age then the months and days.
example: field: date of birth: 1/1/2019
               field: age: 1 year, 1 months and 19 days
               or
    field: age: 1
    field: months: 1
    field: days: 19

My question is how I add the months and days, because I am already years old.
Thanks for your attention.
Luis Ramirez R.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: years of age, months and days

Post by pbottcher » 2020-02-20 18:24

Hi,

you can use

Code: Select all

sql ("update unit set age=TIMESTAMPDIFF(year,DateOfBirth,NOW()),
months=TIMESTAMPDIFF(month,DateOfBirth,NOW()) % 12,
days=FLOOR(TIMESTAMPDIFF(day,DateOfBirth,NOW()) % 30.4375)
where id = '{$ id}'", $ eo);
Where you need to replace the DateOfBirth with the correct variable that you have.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
aarlauskas
Veteran Member
Posts: 127
Joined: 2019-04-28 18:03
Location: Medway, UK

Re: years of age, months and days

Post by aarlauskas » 2020-02-23 22:58

Hi pböttcher, I also need this to bits. Could you help me please please please... I visit some sites where I have special entry ID. The ID is live for 60 days, means after I attend the site, I have to return within 60 days.. If I fail my ID gets parked and there is a long game to get this unparked.. So I would like to have this on my portal as a reminder/tracker. For example when I attend one of these sites, I create a very short record that later displays how many days was since my last visit.
So lets say I create a table and name it: special_sites
Then create few fields: id; site_name; date_attended and days_since
So how would the complete code look in my case? date_attended field will be set as standart date field with date picker, but how do I get the days_since field to display the amount of days since my last visit?
I would really appreciate if you or someone else drops a complete code for my case.
I did try to integrate the above code, but I probably missing the point somewhere.
Thank You.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: years of age, months and days

Post by pbottcher » 2020-02-24 21:32

sql ("update special_sites set days_since=TIMESTAMPDIFF(day,date_attended,NOW()) where id = '{$id}'", $ eo);

with $id having the ID of your selected record.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
aarlauskas
Veteran Member
Posts: 127
Joined: 2019-04-28 18:03
Location: Medway, UK

Re: years of age, months and days

Post by aarlauskas » 2020-02-25 08:09

Cant get this to work. What is the actual top of the code? Can you have a look please? I'm not that sort of expert like you are and still doing stupid mistakes. Can you tidy up this for me please mate? I'm still learinig everyday..

function special_sites_init(&$options, $memberInfo, &$args) {

if (isset ($ _ REQUEST ['SelectedID'])) {
$ id = makeSafe ($ _ REQUEST ['SelectedID']);
$ today = date ('Y-m-d');
sql ("update special_sites set days_since=TIMESTAMPDIFF(day,date_attended,NOW()) where id = '{$ id}'", $ eo);

return TRUE;
}

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: years of age, months and days

Post by jsetzer » 2020-02-25 09:38

Hi,

if this is your code, please first of all remove the blanks/spaces:
function special_sites_init(&$options, $memberInfo, &$args) {

if (isset ($ _ REQUEST ['SelectedID'])) {
$ id = makeSafe ($ _ REQUEST ['SelectedID']);
$ today = date ('Y-m-d');
sql ("update special_sites set days_since=TIMESTAMPDIFF(day,date_attended,NOW()) where id = '{$ id}'", $ eo);

return TRUE;
}
If this is not your code, please paste your real code between [ code ] and [ /code ] tags (without the blanks):

chrome_3mV8dpPpQk.png
chrome_3mV8dpPpQk.png (2.89 KiB) Viewed 10946 times

This would help us helping you.

Best,
Jan
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
aarlauskas
Veteran Member
Posts: 127
Joined: 2019-04-28 18:03
Location: Medway, UK

Re: years of age, months and days

Post by aarlauskas » 2020-02-25 18:46

Hi Jan, No this is not my code, all of this from the above conversation. Something is obviously wrong or stupid. Its just a really simple table with very few fields
special_sites.png
special_sites.png (4.91 KiB) Viewed 10930 times
So what I would like to achieve is:

Field: site_name (Enter site name)
Field: date_attended (enter date of visit. this is date picker, date field)
Field: days_since (I want this field to automaticaly display day count, how many days gone since visit day)
special_sites_hooks.png
special_sites_hooks.png (16.26 KiB) Viewed 10930 times

Code: Select all

	function special_sites_init(&$options, $memberInfo, &$args) {

	if (isset ($_REQUEST ['SelectedID'])) {
	$id = makeSafe ($_REQUEST ['SelectedID']);
	$today = date ('Y-m-d');
	sql ("update special_sites set days_since=TIMESTAMPDIFF(day,date_attended,NOW()) where id = '{$id}'", $eo);

		return TRUE;
	}
Can someone help to sort this code out so it does the job? :roll: I got a feeling the beginning of the code doesnt stick to my sql query.

When I add this code page no longer loading and giving this: Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\portal\hooks\special_sites.php on line 119

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: years of age, months and days

Post by pbottcher » 2020-02-25 20:42

Hi,

Code: Select all

	function special_sites_init(&$options, $memberInfo, &$args) {

	  if (isset ($_REQUEST ['SelectedID'])) {
	    $id = makeSafe ($_REQUEST ['SelectedID']);
	    sql ("update special_sites set days_since=TIMESTAMPDIFF(day,date_attended,NOW()) where id = '{$id}'", $eo);
          }   
		return TRUE;
	}
if (isset ($_REQUEST ['SelectedID'])) {
$id = makeSafe ($_REQUEST ['SelectedID']);
$today = date ('Y-m-d'); // <-------- is not needed
sql ("update special_sites set days_since=TIMESTAMPDIFF(day,date_attended,NOW()) where id = '{$id}'", $eo);
} // <------ this } is missing !!
return TRUE;
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

User avatar
aarlauskas
Veteran Member
Posts: 127
Joined: 2019-04-28 18:03
Location: Medway, UK

Re: years of age, months and days

Post by aarlauskas » 2020-02-25 21:16

I love you man! :D Now we talking. Thank You!!!!

User avatar
aarlauskas
Veteran Member
Posts: 127
Joined: 2019-04-28 18:03
Location: Medway, UK

Re: years of age, months and days

Post by aarlauskas » 2020-02-26 04:48

Hi Buddy. Could you advise me on the following please. I have created two test records. They seems to work fine etc, but then I checked the next day and the days_since field didn't update in table view, it still displays the same amount of days. If I click on the record, then in detail view it displays correct amount of days in that field. I can then go back to table view without saving that record and the days change to correct. Refreshing browser doesn't change anything. I really need this to update in table view instead of going inside the record. There is going to be around 25 records so the idea was to monitor in table view instead of detail view. Thanks
no_time_update.png
no_time_update.png (3.88 KiB) Viewed 10905 times

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: years of age, months and days

Post by pbottcher » 2020-02-26 19:49

Hi,

it would be helpful if you describe at the beginning what you try to acheive.
So in your case you could change the code to:

Code: Select all

	function special_sites_init(&$options, $memberInfo, &$args) {

	    sql ("update special_sites set days_since=TIMESTAMPDIFF(day,date_attended,NOW())'", $eo);

		return TRUE;
	}
This will update your db everytime you call the special_sites page.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

lramirez
Veteran Member
Posts: 46
Joined: 2019-11-01 23:23

Re: years of age, months and days

Post by lramirez » 2020-02-28 16:41

// This works fine, but does not update in detail view, only when I enter the individual data ... what do I do? help .... :-)
// I think I have the same problem the user said: aarlauskas :)

// Year, Month and Days :)

function persons_init(&$options, $memberInfo, &$args) {

if(isset($_REQUEST['SelectedID'])){
$id=makeSafe($_REQUEST['SelectedID']);
$today=date('Y-m-d');

$birth=sqlvalue("SELECT date from persons where id='{$id}'");
$date1 = new DateTime();
$date2 = new DateTime($birth);
$date3 = new DateTime($birth);
$interval = $date1->diff($date2);
$interval = $date1->diff($date3);
$years=$interval->format('%y');
$month=$interval->format('%m');
$days=$interval->format('%d');

sql("update persons set year=floor(datediff('{$today}', date) / 365.25) where id='{$id}'", $eo);

sql("update persons set year='{$years}', month='{$month}', days='{$days}' where id='{$id}'", $eo);
}
return TRUE;
}
Luis Ramirez R.

Post Reply