This topic is locked

Calculating total days between two dates

10/5/2017 9:20:18 AM
PHPRunner General questions
A
admin author

Hi All,
I am using PHPRunner, Can somebody help me as I want to calculate total days between two dates.

I have following 3 fields and 3rd field is just for display
Date_From

Date_to

Total_Datys
When I enter date_from and date_to then It should calculate total days between those two dates and it should place in field 'Total_Days'.
Thanks
Saleem

A
admin author 10/5/2017

Thanks But I am not able to figure out as I tried following but its not working
$time1 = new DateTime('2014-06-10');

$time2 = new DateTime('today');

echo $time2->diff($time1)->days;

HJB 10/5/2017

DATEDIFF(due_date,date_given)
... to be inserted as SQL statement ...

A
Andrew S 10/5/2017

I use the following in one of my projects and it works. May not be the most efficient! This code is in bookings events before record added.
Customer enters Start Date (BookingsStartDate) and End Date (BookingEndDate) once calculation done, the days are stored in BookingNights
The following
$diff = abs(strtotime($values["BookingStartDate"]) - strtotime($values["BookingEndDate"]));

$years = floor($diff / (365606024));

$months = floor(($diff - $years
365606024) / (30606024));

$values["BookingNights"] = floor(($diff - $years 365606024 - $months30606024)/ (606024));
Hope that helps

A
admin author 10/17/2017



I use the following in one of my projects and it works. May not be the most efficient! This code is in bookings events before record added.
Customer enters Start Date (BookingsStartDate) and End Date (BookingEndDate) once calculation done, the days are stored in BookingNights
The following
$diff = abs(strtotime($values["BookingStartDate"]) - strtotime($values["BookingEndDate"]));

$years = floor($diff / (365606024));

$months = floor(($diff - $years
365606024) / (30606024));

$values["BookingNights"] = floor(($diff - $years 365606024 - $months30606024)/ (606024));
Hope that helps


Hi Andrew,
I tried and its not working, Do I need to enter these code server side or Java script, Sorry I am new to PHP Runner
Thanks

A
Andrew S 10/18/2017

You need to make sure you change the field names to whatever your field names (those in bold below) and the code is added via Events -> Before record added
$diff = abs(strtotime($values["BookingStartDate"]) - strtotime($values["BookingEndDate"]));

$years = floor($diff / (365606024));

$months = floor(($diff - $years
365606024) / (30606024));

$values["BookingNights"] = floor(($diff - $years 365606024 - $months30606024)/ (606024));
Andrew

A
admin author 10/18/2017



You need to make sure you change the field names to whatever your field names (those in bold below) and the code is added via Events -> Before record added
$diff = abs(strtotime($values["BookingStartDate"]) - strtotime($values["BookingEndDate"]));

$years = floor($diff / (365606024));

$months = floor(($diff - $years
365606024) / (30606024));

$values["BookingNights"] = floor(($diff - $years 365606024 - $months30606024)/ (606024));
Andrew


Thanks Andrew & appreciate your reponse
But i need to display total days on the screen, Please refer attached screen and the moment i enter Date_to it should calculate total days and calculated todal days hould be displayed in TOTAL DAYS field. Hope I could explain
https://onedrive.live.com/?cid=CE1BBA04BAD8A5B3&id=CE1BBA04BAD8A5B3%2115265&parId=root&o=OneUp
Thanks

Sami

A
Andrew S 10/18/2017

Sami
Your link is not working. However, you will need to use java to achieve what you are trying to do as my method will only work when the record is saved.
https://www.htmlgoodies.com/html5/javascript/calculating-the-difference-between-two-dates-in-javascript.html
Andrew