Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,368
    Thanks
    262
    Thanked 32 Times in 31 Posts

    adding dynamic months to date

    So in my db is a mysql date format table Y m d 2013-02-25

    The post value that comes thru says to add ? months to that date, in this case for testing i want to add 1 month (could be 1-3-6-12)

    so i do this

    PHP Code:

    //dynamic month value from post
    $months $params['number'];
       
    //result 1

    //get current date from db
    $cur_expDate=date('Y-m-d'strtotime($arr_member->mem_expiredate));
      
    // result is  2013-02-25


    //set a value for the function use

    $extenddate '+'.$months.' month';
      
    // result is   +1 month


    add the dynamic month value to the current date 

    $newexpDate 
    date('Y-m-d'strtotime('$extenddate'$cur_expDate));

    //>> Here is the issue this output is  1969-12-31 

    //the result should be  2013-03-25 
    I dont understand if i have already converted it to data format then why is not letting me add to it.
    Last edited by durangod; 02-27-2013 at 03:22 AM.

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    Disregard
    Last edited by Arcticwarrio; 02-26-2013 at 04:48 PM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    sorry ignore that this works instead

    PHP Code:
    //dynamic month value from post
    $months 2;

    //get current date from db
    $cur_expDate=date('Y-m-d'strtotime('2013-02-25'));
      
    // result is  2013-02-25 
    if ($months == 1){
    $newexpDate date('Y-m-d',strtotime('+1 month'strtotime($cur_expDate)));
    }else{
        
    $newexpDate date('Y-m-d',strtotime('+'.$months.' months'strtotime($cur_expDate)));

    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • Users who have thanked Arcticwarrio for this post:

    durangod (02-26-2013)

  • #4
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,368
    Thanks
    262
    Thanked 32 Times in 31 Posts
    so the problem i guess was the fact that i was trying to concatinate the requirement for the first parameter of the function like this

    PHP Code:
    $extenddate '+'.$months.' month'
    instead of actually doing it inside the function itself as you did

    PHP Code:
    strtotime('+'.$months.' months'strtotime($cur_expDate))); 
    and also that i did not use strtotime on the second parameter as well.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    : squint :
    The problem is you are using non-parsable strings:
    PHP Code:
    $newexpDate date('Y-m-d'strtotime('$extenddate'$cur_expDate)); 
    $cur_expDate also won't work. strtotime signature is int strtotime(string, [int]);, so $cur_expDate will be a string causing it to dereference *possibly* from 0 (string conversion to numbers will be adhered to, so in this case it'll likely be 2013 which is only like 33 minutes past epoch).
    PHP Code:
    $iMonths 2;
    $iSetDate strtotime('2013-02-25');
    print 
    date('F j Y h:i:s'strtotime("+$iMonths month"$iSetDate)); 
    Will do what you need.

    DateTime is also an option.
    PHP Code:
    $iMonths 2;
    $dt = new DateTime('2013-02-25');
    $dt->add(new DateInterval("P$iMonthsM"));
    print 
    $dt->format('F j Y h:i:s'); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    durangod (02-27-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •