Times in different element summing

poni

Member
Hi,
In a form and table I have a simple field showing Y-M-D time ( 12-06-27 -forexample), and a calc element showing another Y-M-D time (14-11-29 for example). I need in third (calc ) element ,summing those two times looks like
a : 12-06-27 + 14-11-29 = 27-06-26 summ in third element
How can I summ those two field and show the summ in third calc field ,or show me the link where I can
get a simply answer!
Thanks very much !




 
Last edited:
Well, if I understand correctly, you aren't really "summing" two Y-M-D dates. You are incrementing a date by an interval. So you are adding 14 years, 11 months and 29 days to 2012-06-27.

The way to do that is with a DateInterval. Which means formatting your interval as a string:

http://php.net/manual/en/dateinterval.construct.php

... then using the DateTime add() method ...

http://php.net/manual/en/datetime.add.php

NOTE - you can't use a calc in a calc in Fabrik. So if you need to use (say) calc1 in calc2, you have to re-do the calculation from calc1 in calc2, rather than using a placeholder.

-- hugh
 
Well, if I understand correctly, you aren't really "summing" two Y-M-D dates. You are incrementing a date by an interval. So you are adding 14 years, 11 months and 29 days to 2012-06-27.

The way to do that is with a DateInterval. Which means formatting your interval as a string:

http://php.net/manual/en/dateinterval.construct.php

... then using the DateTime add() method ...

http://php.net/manual/en/datetime.add.php

NOTE - you can't use a calc in a calc in Fabrik. So if you need to use (say) calc1 in calc2, you have to re-do the calculation from calc1 in calc2, rather than using a placeholder.

-- hugh

No , I really want summing that two "string " , and don't now how.
 
OK. Well, you can't just "sum" two YY-MM-DD dates. You have to add an offset to a date. That's just how date arithmetic works, unless you want to figure out all the "how many days in a month" stuff yourself.

So you'll have to use the dateinterval addition method, and then do a little work to remove the milleniums.

So if your dates are in YY-MM-DD format in $datefield1 and $datefield2 ...

Code:
// get date1field from placeholder ... change placeholder name to your full element name
$date1field = '{yourtable___date1}';

// to get $date2field, which is from a calc, you'll have to redo whatever calculation you do in the date2 element again here, and assign it to $date2field
// so YOUR DATE2 CALC CODE goes here, and instead of "return" the result, assign it ...
$date2field = $whatever;

// create the first date object from the Y-m-d format
$date1 = new DateTime::CreateFromFormat('y-m-d', $date1field);

// create the PHP DateInterval string ("PxYxDxH') by exploding the second y-m-d into parts
$date2parts = explode('-', $date2field);
$date2period = "P" . $date2parts[0] . "Y" . $date2parts[1] . "M" . $date2parts[2] . "D";

// add the date interval to the date
$date1->add(new DateInterval($date2period));

// now subtract 2000 years, and then remove leading zeros from a Y (four digit) format.
// So if the two years were 55 and 65, we will have 2120, subtract 2000 and format as Y we get 0120, chop leading 0
$date1->modify('-2000 years');
$datesum = $date1->format('Y-m-d');
$datesum = ltrim($datesum, '0');

// yay, return the result
return $datesum;
 
OK. Well, you can't just "sum" two YY-MM-DD dates. You have to add an offset to a date. That's just how date arithmetic works, unless you want to figure out all the "how many days in a month" stuff yourself.

So you'll have to use the dateinterval addition method, and then do a little work to remove the milleniums.

So if your dates are in YY-MM-DD format in $datefield1 and $datefield2 ...

Code:
// get date1field from placeholder ... change placeholder name to your full element name
$date1field = '{yourtable___date1}';

// to get $date2field, which is from a calc, you'll have to redo whatever calculation you do in the date2 element again here, and assign it to $date2field
// so YOUR DATE2 CALC CODE goes here, and instead of "return" the result, assign it ...
$date2field = $whatever;

// create the first date object from the Y-m-d format
$date1 = new DateTime::CreateFromFormat('y-m-d', $date1field);

// create the PHP DateInterval string ("PxYxDxH') by exploding the second y-m-d into parts
$date2parts = explode('-', $date2field);
$date2period = "P" . $date2parts[0] . "Y" . $date2parts[1] . "M" . $date2parts[2] . "D";

// add the date interval to the date
$date1->add(new DateInterval($date2period));

// now subtract 2000 years, and then remove leading zeros from a Y (four digit) format.
// So if the two years were 55 and 65, we will have 2120, subtract 2000 and format as Y we get 0120, chop leading 0
$date1->modify('-2000 years');
$datesum = $date1->format('Y-m-d');
$datesum = ltrim($datesum, '0');

// yay, return the result
return $datesum;

I put that all in second calc element , rename date1 and date2 , add the code from first calc element(date2) but still nothing. Showing not the summ in format 00-00-00.Show something like the random date in format for.ex. 23.04.2016. Code in first calc element is still there,I rename it in : date2 , still work fine and show calculation.

// get date1field from placeholder ... change placeholder name to your full element name
$date1field = '{radnici___stazaprije}';
//stazaprije is a field element and show quantity of years,months,days(for ex. 11-06-22)

// to get $date2field, which is from a calc, you'll have to redo whatever calculation you do in the date2 element again here, and assign it to $date2field
// so YOUR DATE2 CALC CODE goes here, and instead of "return" the result, assign it ...

//this below is from first calc element : prvidanrada is DATE element in list radnici (formatdate is 04-07-2016----fourt july)

$start_date2field = '{radnici___prvidanrada}';
if (!empty($start_date2field)) {
$now_date = new DateTime();
$start_date2field = new DateTime($start_date2field);
$since_start = $start_date2field->diff($now_date);
return $since_start->format('%y-%m-%d');
}
else {
$date2field = $whatever;
}


// create the first date object from the Y-m-d format
$date1 = new DateTime::CreateFromFormat('y-m-d', $date1field);

// create the PHP DateInterval string ("PxYxDxH') by exploding the second y-m-d into parts
$date2parts = explode('-', $date2field);
$date2period = "P" . $date2parts[0] . "Y" . $date2parts[1] . "M" . $date2parts[2] . "D";

// add the date interval to the date
$date1->add(new DateInterval($date2period));

// now subtract 2000 years, and then remove leading zeros from a Y (four digit) format.
// So if the two years were 55 and 65, we will have 2120, subtract 2000 and format as Y we get 0120, chop leading 0
$date1->modify('-2000 years');
$datesum = $date1->format('Y-m-d');
$datesum = ltrim($datesum, '0');

// yay, return the result
return $datesum;
 
Last edited:
I've gone as far as I can with this on free Community support. If you need me to write this code for you, you'll need to get subscription.

That said, your code in pink needs to set $date2field to the value your first calc would have returned, NOT return a value, and NOT set it to $whatever (which I was just using as shorthand for "set $date2field to whatever your first calc would have returned").

-- hugh
 
I
I've gone as far as I can with this on free Community support. If you need me to write this code for you, you'll need to get subscription.

That said, your code in pink needs to set $date2field to the value your first calc would have returned, NOT return a value, and NOT set it to $whatever (which I was just using as shorthand for "set $date2field to whatever your first calc would have returned").

-- hugh

Hugh , I tried everything ,but not became a format like : 12-05-17 or 06-10-22. Became a random date number in format : 23.07.2016. or 11.04.2014. I put whole code , what you wrote me ,and
change the name of datefield1 and datefield2.What is wrong . Did I miss something.


// get date1field from placeholder ... change placeholder name to your full element name
$date1field = '{radnici___stazaprije}';


// to get $date2field, which is from a calc, you'll have to redo whatever calculation you do in the date2 element again here, and assign it to $date2field
// so YOUR DATE2 CALC CODE goes here, and instead of "return" the result, assign it ...

$date2field = $'{radnici___prvidanrada}';

$start_date2field = '{radnici___prvidanrada}';
if (!empty($start_date2field)) {
$now_date = new DateTime();
$start_date2field = new DateTime($start_date2field);
$since_start = $start_date2field->diff($now_date);
return $since_start->format('%y-%m-%d');
}
else {
return "no data";
}

// create the first date object from the Y-m-d format
$date1 = new DateTime::CreateFromFormat('y-m-d', $date1field);

// create the PHP DateInterval string ("PxYxDxH') by exploding the second y-m-d into parts
$date2parts = explode('-', $date2field);
$date2period = "P" . $date2parts[0] . "Y" . $date2parts[1] . "M" . $date2parts[2] . "D";

// add the date interval to the date
$date1->add(new DateInterval($date2period));

// now subtract 2000 years, and then remove leading zeros from a Y (four digit) format.
// So if the two years were 55 and 65, we will have 2120, subtract 2000 and format as Y we get 0120, chop leading 0
$date1->modify('-2000 years');
$datesum = $date1->format('Y-m-d');
$datesum = ltrim($datesum, '0');

// yay, return the result
return $datesum ;
 
I'm sorry, as I said in my last post, I've gone as far as I can in community support. Well, further than I can go, really. I just can't afford to write and debug custom code for free. As I said, if you get a support sub, I'll do what I can to help.
-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top