phpnoobietoo Posted February 16, 2012 Share Posted February 16, 2012 i wonder if anyone can help with this problem. I have a form where the dates are set (and have to be passed) in the format: &checkin_monthday=1&checkin_year_month=2012-3&checkout_monthday=2&checkout_year_month=2012-3 so my form is like this:<select id="b_checkin_month" name="checkin_year_month" onchange="checkDateOrder('b_availFrm2', 'b_checkin_day', 'b_checkin_month', 'b_checkout_day', 'b_checkout_month');"> <option value="2012-2">February '12</option> <option value="2012-3">March '12</option> <option value="2012-4">April '12</option> <option value="2012-5">May '12</option> <option value="2012-6">June '12</option> <option value="2012-7">July '12</option> <option value="2012-8">August '12</option> <option value="2012-9">September '12</option> <option value="2012-10">October '12</option> <option value="2012-11">November '12</option> <option value="2012-12">December '12</option> <option value="2013-1">January '13</option> </select> and then every month i have to manually delete the top month and add a new month at the end. is there anyway to automate this with php rather than the javascript route which means users with scripting disabled cant use the form. I saw something that suggested you can do a check for current month/year with php and then set code based on the result but understandng what to do was beyond me im afraid. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/ Share on other sites More sharing options...
scootstah Posted February 16, 2012 Share Posted February 16, 2012 This ought to do it: echo '<select>'; $option = ''; for($i = 0; $i <= 11; $i++) { $timestamp = strtotime('+' . $i . ' month'); $month_text = date('F', $timestamp); $month_num = date('n', $timestamp); $year = date('Y', $timestamp); $option .= sprintf('<option value="%d-%d">%s</option>', $year, $month_num, $month_text); } echo $option; echo '</select>'; EDIT: Updated. Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/#findComment-1318091 Share on other sites More sharing options...
phpnoobietoo Posted February 16, 2012 Author Share Posted February 16, 2012 That works perfect thank you. Now i need to work out exactly how the code worked. Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/#findComment-1318101 Share on other sites More sharing options...
scootstah Posted February 16, 2012 Share Posted February 16, 2012 Here, I've added some comments to (hopefully) help // start a for loop to increment an integer // $i will start at 0 and end at 11 (so 12 increments) for($i = 0; $i <= 11; $i++) { // $timestamp will contain the UNIX timestamp returned by strtotime // by default, strtotime works off the current time // so in this case we are adding X months to the current month // the amount of months is determined by $i which will be // incremented with each pass of the for loop, starting with 0 $timestamp = strtotime('+' . $i . ' month'); // here we are just getting the different formats of the date // from the UNIX timestamp that we got above $month_text = date('F', $timestamp); $month_num = date('n', $timestamp); $year = date('Y', $timestamp); // and finally, we append a formatted string to $option $option .= sprintf('<option value="%d-%d">%s</option>', $year, $month_num, $month_text); } Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/#findComment-1318104 Share on other sites More sharing options...
Psycho Posted February 16, 2012 Share Posted February 16, 2012 I think there might be a problem with that code. If it was Jan 31st, and you did strtotime('+1 month') it will skip February. I'm having problems with my dev environment right now, But, I'm pretty sure I've seen this behavior before. You could set the optional parameter for strtotime() to be the 1st of the current month. Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/#findComment-1318108 Share on other sites More sharing options...
phpnoobietoo Posted February 16, 2012 Author Share Posted February 16, 2012 can i ask how i would do that? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/#findComment-1318127 Share on other sites More sharing options...
Psycho Posted February 16, 2012 Share Posted February 16, 2012 Again, I would first test it to see if my understanding is correct (like I said I'm having problems right now, so I can't). But, if the problem does exist, you just need to set an initial timestamp where adding '+1 month' will get you the results you want. But, I think I was mistaken about setting to the 1st, the 15th might be a better option. I'll see about uploading files to a different environment and testing this and I'll respond back on whether any changes are needed. Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/#findComment-1318132 Share on other sites More sharing options...
phpnoobietoo Posted February 16, 2012 Author Share Posted February 16, 2012 i tried this with wamp on my local pc and setting the clock to jan 31st and the code seemed to work fine as posted originally. Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/#findComment-1318133 Share on other sites More sharing options...
Psycho Posted February 16, 2012 Share Posted February 16, 2012 i tried this with wamp on my local pc and setting the clock to jan 31st and the code seemed to work fine as posted originally. Hmm, may be a version issue with PHP. I just tested it and got incorrect results. I tested the original code using a "seed" date of Jan 31 and I tested the actual result of the timestamp created in the loop and it went as follows: Jan 1, March 2, then March 31. It completely skipped Feb and had two results for March. When I tested it by "normalizing" the date to the 1st of the month it worked correctly. //Create a seed timestamp for 1st of current month $start_month = strtotime(date('M 1, Y')); // start a for loop to increment an integer // $i will start at 0 and end at 11 (so 12 increments) for($i = 0; $i <= 11; $i++) { // $timestamp will contain the UNIX timestamp returned by strtotime // by default, strtotime works off the current time // so in this case we are adding X months to the current month // the amount of months is determined by $i which will be // incremented with each pass of the for loop, starting with 0 $timestamp = strtotime('+' . $i . ' month', $start_month); // here we are just getting the different formats of the date // from the UNIX timestamp that we got above $month_text = date('F', $timestamp); $month_num = date('n', $timestamp); $year = date('Y', $timestamp); // and finally, we append a formatted string to $option $option .= sprintf("<option value='%d-%d'>%s</option>\n", $year, $month_num, $month_text); } Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/#findComment-1318136 Share on other sites More sharing options...
Psycho Posted February 16, 2012 Share Posted February 16, 2012 Note: the manual even contains a user submitted comment regarding this issue http://www.php.net/manual/en/function.strtotime.php#107331 WARNING when using "next month", "last month", "+1 month", "-1 month" or any combination of +/-X months. It will give non-intuitive results on Jan 30th and 31st. As described at : http://derickrethans.nl/obtaining-the-next-month-in-php.html <?php $d = new DateTime( '2010-01-31' ); $d->modify( 'next month' ); echo $d->format( 'F' ), "\n"; ?> In the above, using "next month" on January 31 will output "March" even though you might want it to output "February". ("+1 month" will give the same result. "last month", "-1 month" are similarly affected, but the results would be seen at beginning of March.) The way to get what people would generally be looking for when they say "next month" even on Jan 30 and Jan 31 is to use "first day of next month": Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/#findComment-1318139 Share on other sites More sharing options...
phpnoobietoo Posted February 16, 2012 Author Share Posted February 16, 2012 Thank you so much for taking the time to test and amend the code. They dont call you guru for nothing! Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/#findComment-1318140 Share on other sites More sharing options...
phpnoobietoo Posted February 17, 2012 Author Share Posted February 17, 2012 something strange happened when i put this live onto the website. Click throughs dropped by half. I switched back to the old form and they went back to normal. Im wondering, can the users geographical location have any impact on the resulting formatting or any other aspects of the script? I understand everything is parsed on my server so i dont see how it would impact. Very strange. Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/#findComment-1318465 Share on other sites More sharing options...
Psycho Posted February 17, 2012 Share Posted February 17, 2012 something strange happened when i put this live onto the website. Click throughs dropped by half. I switched back to the old form and they went back to normal. Im wondering, can the users geographical location have any impact on the resulting formatting or any other aspects of the script? I understand everything is parsed on my server so i dont see how it would impact. Very strange. I wouldn't see how that code would cause a problem. The date/time functions should be working off of the server. But, that could cause a slight problem around the beginning/end of the month. For example, if the server is 12 hours ahead of the user, on the last day of the month that month would not be available for that user after noon on that day. Other than that, I don't know why you would have a problem. Have you tested it? Try changing the timezone on your PC to see if there are any problems. Quote Link to comment https://forums.phpfreaks.com/topic/257125-setting-form-dates/#findComment-1318473 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.