Jump to content

setting form dates


phpnoobietoo

Recommended Posts

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 &#39;12</option>

<option value="2012-3">March &#39;12</option>

<option value="2012-4">April &#39;12</option>

<option value="2012-5">May &#39;12</option>

<option value="2012-6">June &#39;12</option>

<option value="2012-7">July &#39;12</option>

<option value="2012-8">August &#39;12</option>

<option value="2012-9">September &#39;12</option>

<option value="2012-10">October &#39;12</option>

<option value="2012-11">November &#39;12</option>

<option value="2012-12">December &#39;12</option>

<option value="2013-1">January &#39;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.

:confused:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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":

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.