Calculate and display days between 2 dates

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
User avatar
bruceholt
Veteran Member
Posts: 100
Joined: 2016-07-30 20:16
Location: Australia

Calculate and display days between 2 dates

Post by bruceholt » 2019-06-16 00:59

Hi,

I am trying to calculate numbers of days between 2 dates.

In the table named "livestock_movements", "date_in" is the start date and "date_out" is the end date. I have a field named "days_in" which I would like to show the days between the two dates, but can't get it to work.

I have in the hooks file livestock_movements.php this code:

Code: Select all

function livestock_movements_after_insert($data, $memberInfo, &$args){
		
		$days_in = (strtotime($data['date_out'])- strtotime($data['date_in']))/86400;

		return TRUE;
	}

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

Re: Calculate and display days between 2 dates

Post by pbottcher » 2019-06-16 20:12

Hi,

maybe this gets you further

viewtopic.php?f=7&t=3004&p=9972#p9954
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
bruceholt
Veteran Member
Posts: 100
Joined: 2016-07-30 20:16
Location: Australia

Re: Calculate and display days between 2 dates

Post by bruceholt » 2019-06-29 03:47

Hi pböttcher,

Thanks for your reply but for some reason that confused me. What I am wanting to do is work out the days between two dates and hopefully store it in "days_in"

Sorry to be a pain.

Thanks, Bruce

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

Re: Calculate and display days between 2 dates

Post by pbottcher » 2019-06-29 07:34

Hi,

try this

Code: Select all

function livestock_movements_after_insert($data, $memberInfo, &$args){
		$date1 = new DateTime($data['date_in']);
		$date2 = new DateTime($data['date_out']);
		$interval = $date1->diff($date2);
		$days=$interval->days;

// assuming you want to set a field in the livestock_movements table for the seleceted ID
		$sql="Update livestock_movements set YOURFIELD = ".$days." where id = ".$data['selectedID'];

		sqlvalue($sql);
		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
bruceholt
Veteran Member
Posts: 100
Joined: 2016-07-30 20:16
Location: Australia

Re: Calculate and display days between 2 dates

Post by bruceholt » 2019-06-29 09:21

Hi pböttcher,

That worked beautifully. Thank you very much for your help again.

Bruce

Post Reply