A JM Posted December 20, 2009 Share Posted December 20, 2009 I'm trying to add 30 days to a date and not having any success - the formula looks correct but the results are incorrect. Can someone help? $duedate = date("m/d/Y", strtotime($row['inv_submitted'])); $duedate = $duedate + date("m/d/Y", strtotime("+30 day")); A JM, Quote Link to comment https://forums.phpfreaks.com/topic/185777-problem-adding-30-days-to-date/ Share on other sites More sharing options...
Buddski Posted December 20, 2009 Share Posted December 20, 2009 date('m/d/y',strtotime('+30 days',strtotime($row['inv_submitted']))); Perhaps? Quote Link to comment https://forums.phpfreaks.com/topic/185777-problem-adding-30-days-to-date/#findComment-980952 Share on other sites More sharing options...
A JM Posted December 20, 2009 Author Share Posted December 20, 2009 Many thanks. Out of curiosity why didn't my formula work it appears to be adding both strtotime() functions together?? A JM, Quote Link to comment https://forums.phpfreaks.com/topic/185777-problem-adding-30-days-to-date/#findComment-980955 Share on other sites More sharing options...
PFMaBiSmAd Posted December 20, 2009 Share Posted December 20, 2009 Using two strtotime() functions is doubly-slow. If the original date can be understood by strtotime(), just do it all at once - $duedate = date('m/d/y',strtotime("{$row['inv_submitted']} +30 days")); If your inv_submitted field is in fact a DATE data type, just use the mysql DATE_ADD() function or directly add an INTERVAL in your query. To get the value formatted as a 'm/d/y', just use the mysql DATE_FORMAT() function in your query. Quote Link to comment https://forums.phpfreaks.com/topic/185777-problem-adding-30-days-to-date/#findComment-980956 Share on other sites More sharing options...
Buddski Posted December 20, 2009 Share Posted December 20, 2009 Sure.. For starters both of these declarations return strings.. So the first one will return, for example, 12/3/2009, and the second will return just add 30 days to todays date.. and you are trying to add them together which would never work... $duedate = date("m/d/Y", strtotime($row['inv_submitted'])); $duedate = $duedate + date("m/d/Y", strtotime("+30 day")); Have a read of this http://php.net/manual/en/function.strtotime.php for a better understanding of the strtotime function.. Quote Link to comment https://forums.phpfreaks.com/topic/185777-problem-adding-30-days-to-date/#findComment-980959 Share on other sites More sharing options...
A JM Posted December 20, 2009 Author Share Posted December 20, 2009 ah, makes sense I missed the adding strings together part, duh.. thanks. Sure.. For starters both of these declarations return strings.. So the first one will return, for example, 12/3/2009, and the second will return just add 30 days to todays date.. and you are trying to add them together which would never work... $duedate = date("m/d/Y", strtotime($row['inv_submitted'])); $duedate = $duedate + date("m/d/Y", strtotime("+30 day")); Have a read of this http://php.net/manual/en/function.strtotime.php for a better understanding of the strtotime function.. Quote Link to comment https://forums.phpfreaks.com/topic/185777-problem-adding-30-days-to-date/#findComment-980960 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.