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
years of age, months and days
years of age, months and days
Luis Ramirez R.
Re: years of age, months and days
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()
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.
Re: years of age, months and days
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.
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.
Re: years of age, months and days
Hi,
you can use
Where you need to replace the DateOfBirth with the correct variable that you have.
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);
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.
- aarlauskas
- Veteran Member
- Posts: 127
- Joined: 2019-04-28 18:03
- Location: Medway, UK
Re: years of age, months and days
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.
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.
Re: years of age, months and days
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.
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.
- aarlauskas
- Veteran Member
- Posts: 127
- Joined: 2019-04-28 18:03
- Location: Medway, UK
Re: years of age, months and days
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;
}
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;
}
Re: years of age, months and days
Hi,
if this is your code, please first of all remove the blanks/spaces:
This would help us helping you.
Best,
Jan
if this is your code, please first of all remove the blanks/spaces:
If this is not your code, please paste your real code between [ code ] and [ /code ] tags (without the blanks):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;
}
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
AppGini 24.10 Revision 1579 + all AppGini Helper tools
<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 readabilityAppGini 24.10 Revision 1579 + all AppGini Helper tools
- aarlauskas
- Veteran Member
- Posts: 127
- Joined: 2019-04-28 18:03
- Location: Medway, UK
Re: years of age, months and days
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
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)
Can someone help to sort this code out so it does the job? 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
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)
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;
}
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
Re: years of age, months and days
Hi,
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;
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;
}
$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.
- aarlauskas
- Veteran Member
- Posts: 127
- Joined: 2019-04-28 18:03
- Location: Medway, UK
Re: years of age, months and days
I love you man! Now we talking. Thank You!!!!
- aarlauskas
- Veteran Member
- Posts: 127
- Joined: 2019-04-28 18:03
- Location: Medway, UK
Re: years of age, months and days
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
Re: years of age, months and days
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:
This will update your db everytime you call the special_sites page.
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;
}
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.
Re: years of age, months and days
// 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;
}
// 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.