Forums

DATEDIFF(d1,d2)

webcrea 11 Jan, 2019
Hi,

I would to save a dif between 2 date

in Data override on Update

duration:DATEDIFF(end_date, start_date)

but chrono send query to mysql with '

`duration` = 'DATEDIFF(end_date, start_date)'

Thanks
healyhatman 11 Jan, 2019
Do the date difference in PHP.
webcrea 11 Jan, 2019
Hi healyhatman,

in chrono function php

$d1 = new DateTime( $this->get('d1') );
$d2 = new DateTime( $this->get('d2') );
$interval = $d2->diff($d1);
echo $interval;

produces a white screen !!!
healyhatman 11 Jan, 2019
Well of course, don't you mean $this->data ? And you'll want to check to make sure d1 and D2 end up as valid dates first before trying diff
webcrea 11 Jan, 2019
$d1 = new DateTime( $this->get('d1') );
$d2 = new DateTime( $this->get('d2') );
//$interval = $d2->diff($d1);

var_dump( $d1 > $d2 );

echo "<p>" . $this->get('d1');
echo "<p>" . $this->get('d2');
echo $interval;

result is :

bool(true)

2018-12-31
2018-12-01



this is correct...
healyhatman 11 Jan, 2019
diff() returns an object, which you can't store in the database. You want to return
$interval->format('format string');
http://php.net/manual/en/datetime.diff.php
webcrea 11 Jan, 2019
$d1 = new DateTime( $this->get('d1') );
$d2 = new DateTime( $this->get('d2') );

$interval = $d2->diff($d1);
$interval->format("%a");

echo $interval;


always white screen
healyhatman 11 Jan, 2019
Turn your error reporting on then buddy to find the problem
webcrea 11 Jan, 2019
Catchable fatal error: Object of class DateInterval could not be converted to string in ...../libraries/cegcore2/admin/extensions/chronofc/functions/php/php_output.php(6) : eval()'d code on line 7
healyhatman 11 Jan, 2019
1 Likes
Yeah you didn't assign the result of ->format to $interval. when you echo'd it's still an interval object.

So need
return $interval->format(....)
Or
echo $interval->format
Or
$interval = $interval->format
webcrea 11 Jan, 2019
Yes,
Thank you very much
Great
webcrea 11 Jan, 2019
Finaly to calculate a diff between 2 date and save it in "save data"

Create a php function date_diff with

$d1 = new DateTime( $this->get('d1') );
$d2 = new DateTime( $this->get('d2') );

$interval = $d2->diff($d1);
echo $interval->format("%a");

-------------------------------------------
And in in Data override on Update

duration:{fn:date_diff$d1=(data:campaign.end_date)&d2=(data:campaign.start_date)}
This topic is locked and no more replies can be posted.