This topic is locked
[SOLVED]

 How to work with date fields and date_diff function?

3/3/2011 8:39:36 PM
PHPRunner General questions
C
caese author

Hello:

After an input, I need to obtain the days between two dates (fields fdesde and fhasta - dd/mm/yyyy - take with the datepicker).

I found the date_diff function in the php manual

This is the code I put in Add Page - Before record added.
$values["diferencia"] = date_diff($values["fdesde"],$values["fhasta"]);
And this is the error message:
date_diff() expects parameter 1 to be DateTime, string given
How I convert the fields to date on the fly?

or

Exist another way to obtain the days between two dates?
Sorry about my english.

Admin 3/3/2011

Check this article:

http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php
The key is to use strtotime() function to convert both dates to timestamp. Then you can find the difference between dates.

$diff = abs(strtotime($values["fdesde"]) - strtotime($values["fhasta"]));

$years = floor($diff / (365*60*60*24));

$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));

$values["diferencia"] = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));