Page 1 of 1

Calculate and display days between 2 dates

Posted: 2019-06-16 00:59
by bruceholt
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;
	}

Re: Calculate and display days between 2 dates

Posted: 2019-06-16 20:12
by pbottcher
Hi,

maybe this gets you further

viewtopic.php?f=7&t=3004&p=9972#p9954

Re: Calculate and display days between 2 dates

Posted: 2019-06-29 03:47
by bruceholt
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

Re: Calculate and display days between 2 dates

Posted: 2019-06-29 07:34
by pbottcher
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;
	}

Re: Calculate and display days between 2 dates

Posted: 2019-06-29 09:21
by bruceholt
Hi pböttcher,

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

Bruce