Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-26-2013, 04:26 PM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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..
durangod is offline   Reply With Quote
Old 02-26-2013, 04:38 PM   PM User | #2
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 574
Thanks: 15
Thanked 64 Times in 64 Posts
Arcticwarrio is on a distinguished road
Disregard
__________________
There are 10 types of people on CodingForums,
Those who understand Binary and those who dont.

Last edited by Arcticwarrio; 02-26-2013 at 04:48 PM..
Arcticwarrio is offline   Reply With Quote
Old 02-26-2013, 04:47 PM   PM User | #3
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 574
Thanks: 15
Thanked 64 Times in 64 Posts
Arcticwarrio is on a distinguished road
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.
Arcticwarrio is offline   Reply With Quote
Users who have thanked Arcticwarrio for this post:
durangod (02-26-2013)
Old 02-26-2013, 05:03 PM   PM User | #4
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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.
durangod is offline   Reply With Quote
Old 02-26-2013, 05:44 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
: 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'); 
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
durangod (02-27-2013)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:25 AM.


Advertisement
Log in to turn off these ads.