deltajam_v Posted April 18, 2006 Share Posted April 18, 2006 Hi, I'm pulling a datetime field called 'expires' from the database. I also have a field in the db called 'months' that I would like to add to the expires field.So if:$expires = 2006-04-17 09:54:23;$months = 6;I want to add the months to the expires variable. How would I do that? Quote Link to comment https://forums.phpfreaks.com/topic/7722-php-date/ Share on other sites More sharing options...
wisewood Posted April 18, 2006 Share Posted April 18, 2006 I hope I understand you correctly. If so, this will do what you want.[code]<?php // Your original data $expires = "2006-04-17 09:54:23"; $months = "6"; // Break the $expires variable into component parts $year = substr($expires, 0, 4); $month = substr($expires, 5, 2); $day = substr($expires, 8, 2); $hour = substr($expires, 11, 2); $minute = substr($expires, 14, 2); $second = substr($expires, 17, 2); // Add $months onto $month $new_month = $months+$month; // If $new_month is only 1 digit, add a preceeding zero $strlen = strlen($new_month); if($strlen==1) {$new_month = "0$new_month";} // Set the $expires variables with the new value $expires = "$year-$new_month-$day $hour:$minute:$second"; // Just so you can see that it works... echo the result. echo "$expires";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/7722-php-date/#findComment-28181 Share on other sites More sharing options...
kenrbnsn Posted April 18, 2006 Share Posted April 18, 2006 Just use the [a href=\"http://www.php.net/strtotime\" target=\"_blank\"]strtotime()[/a] function to add the months:[code]<?php // Your original data $expires = "2006-04-17 09:54:23"; $months = "6"; $expiration_date = date('Y-m-d H:i:s',strtotime($expires . ' + ' . $months . ' + months')); echo $expires . ' + ' . $months . ' months = ' . $expiration_date;?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/7722-php-date/#findComment-28187 Share on other sites More sharing options...
deltajam_v Posted April 18, 2006 Author Share Posted April 18, 2006 Thank you very much kenrbnsn. Not to discredit wisewood though ... both work perfectly. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/7722-php-date/#findComment-28192 Share on other sites More sharing options...
wisewood Posted April 18, 2006 Share Posted April 18, 2006 Don't worry... ken knows more clever stuff than i do Quote Link to comment https://forums.phpfreaks.com/topic/7722-php-date/#findComment-28208 Share on other sites More sharing options...
deltajam_v Posted November 21, 2006 Author Share Posted November 21, 2006 When I use kenrbnsn's code, the output is pretty wacky:2006-04-17 09:54:23 + 6 months = 1969-12-31 19:00:00any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/7722-php-date/#findComment-127958 Share on other sites More sharing options...
kenrbnsn Posted November 21, 2006 Share Posted November 21, 2006 That will happen when the strtotime() functon does not recognize the input as a valid date/time string. I just tested the code again and it works fine. Please post the code you used to get the invalid result. Also what changed since April when you said it worked fine.Ken Quote Link to comment https://forums.phpfreaks.com/topic/7722-php-date/#findComment-127963 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.