1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

adding a dates

Discussion in 'PHP' started by nipo, Jan 26, 2017.

  1. #1
    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 acode :



    $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.
    Thanks anyway.
     
    nipo, Jan 26, 2017 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Uhm... What? Do you want the final calculation to be an actual date, or just adding the separate numbers together? If the latter, you'll have to split the string up and add each pair, before outputting the new values.
     
    PoPSiCLe, Jan 26, 2017 IP
  3. nipo

    nipo Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    I want just adding the separate numbers together. I've got a good solution in excel , but (Y-m-d) is in 3 column,and solved with excel formulas.Thanks.
     
    nipo, Jan 26, 2017 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    For what it's worth, in PHP you could do something like this:
    
    $date1 = explode('-','29-04-10'); // you can use variables instead of the actual dates, of course
    $date2 = explode('-','13-05-16');
    echo $total = join('-',[$date1[0]+$date2[0],$date1[1]+$date2[1],$date1[2]+$date2[2]]);
    
    PHP:
    The above will output this:
    
    42-9-26
    
    Code (markup):
    And if you want leading zeros, change the $total from the above to this:
    
    echo $total = join('-',[sprintf('%02d', $date1[0]+$date2[0]),sprintf('%02d', $date1[1]+$date2[1]),sprintf('%02d', $date1[2]+$date2[2])]);
    PHP:
    Which will output this:
    
    42-09-26
    
    Code (markup):
     
    Last edited: Jan 26, 2017
    PoPSiCLe, Jan 26, 2017 IP
  5. nipo

    nipo Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    How to use variables.If I could understand , this is two solutions.Can i change '29-04-10' and '13-05-16' in first solutions with yourtable___yourdate , or with Y-m-d. I tried write that in my fourth field (calc element).
    but nothing happenedThird field is only field and number is in text format.Second field is calc element with code.Fourth field is also could be a calc element with code you give me,but nothing.How you get the result.
     
    nipo, Jan 28, 2017 IP
  6. nipo

    nipo Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    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 =newDateTime();
    $start_date =newDateTime($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!
     
    nipo, Jan 28, 2017 IP
  7. nipo

    nipo Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #7
    Please , will you clarify me how to set variables to get asolutions. Please help me !
     
    nipo, Mar 7, 2017 IP
  8. yedort

    yedort Member

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    33
    #8
    Why don't you do your calculations with the time() function?

    Edit: I guess it doesn't matter if it's a non-existing date.

    How about:

    $finalval = ((int)gmdate('d', $alinuxtime) + (int)gmdate('d', $anotherlinuxtime)).'-'.((int)gmdate('m', $alinuxtime) + (int)gmdate('m', $anotherlinuxtime)).'-'.((int)gmdate('Y', $alinuxtime) + (int)gmdate('Y', $anotherlinuxtime));
     
    Last edited: Mar 8, 2017
    yedort, Mar 8, 2017 IP
  9. nipo

    nipo Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #9
    ????? What is "alinuxtime" and "anotherlinuxtime" ?
     
    nipo, Mar 8, 2017 IP
  10. yedort

    yedort Member

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    33
    #10
    $start_date and $since_start
     
    yedort, Mar 8, 2017 IP
  11. yedort

    yedort Member

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    33
    #11
    I didn't see PoPSiCLe's answer, it works well too.
     
    yedort, Mar 9, 2017 IP
  12. nipo

    nipo Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #12
    Yedort thanks , but i think ; useful solution is PoPSiCLe solution,but I am beginner , and don't know set the variables,and must be taken into consideration and resolution of the CALC field.Just adding the separate numbers together in the fourth calc field.All that is in column in database table!Never mind , thanks!
    PS. I must put a column_name instead of these 29-04-10 and 13-05-16.
     
    Last edited: Mar 9, 2017
    nipo, Mar 9, 2017 IP
  13. nipo

    nipo Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #13
    Does anyone know what I'm talking about???Thanks.
     
    nipo, Mar 16, 2017 IP