Jump to content

summing two dates


nipo

Recommended Posts

  Hi !

I have a litle problem.

I have on my form four field. In first I have a birthdays in format  d-m-Y(12-03-1994). Second field calculatig age from birthday until now ,formated Y-m-d (29-04-10  for example), and  have a code :

 

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

 

and work fine .

 

PROBLEM IS :  In third field have another age in format Y-m-d  for example 13-05-16 , and  want summ with second field.   (29-04-10 + 13-05-16 = 42-09-26)

The summ of calculating  42-09-26  (Y-m-d) must be displayed in fourth field ,but how.

Link to comment
Share on other sites

I have a first field on form with job beginning in date format d-m-Y (27-03-1997 for example).Second calc field is with code and can calculate a length of sevice from beginning (27-03-1997) until today.Calc field show me result in Y-m-d format(19-03-29) and work fine.Here is the code:

 

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

 

Now, I have another length of service (03-08-02 for example) in same form, which is done before 1997. ,and wrote in simple third field in text format(Y-m-d). I want in fourth calc field on form get the summ of those two length of service. Exactly : 24-00-01 must be result (19-03-29 + 03-08-02).One month is 30 days.Form is on Joomla 2.5 , made with form builder. Thanks for help!

Link to comment
Share on other sites

This still doesn't make much sense, and there's no PHP function which could do that for you.

 

The DateTime::diff() method calculates the actual number of days, months and years between two dates. If you want to “add” another interval using your own logic (“one month is 30 days”), you have to do that yourself. That is, you must add the days of both intervals, divide the result by 30, then add the months etc.

 

Like I said, there is no PHP function for this kind of logic.

Edited by Jacques1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.