Page 1 of 1

years of age, months and days

Posted: 2020-02-16 17:42
by lramirez
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

Re: years of age, months and days

Posted: 2020-02-16 18:39
by pbottcher
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()

Re: years of age, months and days

Posted: 2020-02-19 19:06
by lramirez
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.

Re: years of age, months and days

Posted: 2020-02-20 18:24
by pbottcher
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.

Re: years of age, months and days

Posted: 2020-02-23 22:58
by aarlauskas
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.

Re: years of age, months and days

Posted: 2020-02-24 21:32
by pbottcher
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.

Re: years of age, months and days

Posted: 2020-02-25 08:09
by aarlauskas
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;
}

Re: years of age, months and days

Posted: 2020-02-25 09:38
by jsetzer
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 12364 times

This would help us helping you.

Best,
Jan

Re: years of age, months and days

Posted: 2020-02-25 18:46
by aarlauskas
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 12348 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 12348 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

Re: years of age, months and days

Posted: 2020-02-25 20:42
by pbottcher
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;

Re: years of age, months and days

Posted: 2020-02-25 21:16
by aarlauskas
I love you man! :D Now we talking. Thank You!!!!

Re: years of age, months and days

Posted: 2020-02-26 04:48
by aarlauskas
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 12323 times

Re: years of age, months and days

Posted: 2020-02-26 19:49
by pbottcher
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.

Re: years of age, months and days

Posted: 2020-02-28 16:41
by lramirez
// 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;
}