Jump to content

date() month displaying incorrectly


itchy8me

Recommended Posts

Hello

 

I'm having problems with a piece of code i wrote (pasted in the code quote). Its been working for a month now on my webserver and all of a sudden today it doesn't work anymore. The code iterates ($i) the month in mktime() to create a date() and extract the month from it. However it has now started printing 1 month number for every two iterations as shown in the screen capture below( the number after the dash is the value of $i).

 

page.jpeg

 

	echo "</SELECT>-
	<SELECT name=\"ETA_month$ii\">";
				for($i = 1; $i <= 12; ++$i)
				{
					echo "<OPTION value=\"";
					echo date("m", mktime(date("H"), date("i"), date("s"), $i, date("d"), date("Y")));
					echo "\"";
					if($time_1_parse[month] == date("m", mktime(date("H"), date("i"), date("s"), $i, date("d"), date("Y"))))
					{
						echo " selected='true'";
					};
					echo ">";
					echo date("m", mktime(date("H"), date("i"), date("s"), $i, date("d"), date("Y")));
					echo "-$i</OPTION>";
					//echo date("m", mktime(date("H"), date("i"), date("s"), $i, date("d"), date("Y")));
				}
	echo "</SELECT>";

 

am i doing something wrong that i overlooked? or is this a bug?

 

kind regards,

Wernher

Link to comment
Share on other sites

okay, i think i'm not understanding something about the functions date() or mktime().

 

Having certain suspicions about the above mentioned code, i left the code(in the 1st post) running on my localhost to see what would happen when the month turned 09.. i nearly lost my head.. the iteration of the month now has one entry per month again.. like they where doing yesterday and a month before.. wtf!? what is it  that am doing wrong in the above code?

Link to comment
Share on other sites

nice programming tip thanks, i have now changed the code to the following:

echo "</SELECT>-
	<SELECT name=\"ETA_month$ii\">";
				for($i = 1; $i <= 12; ++$i)
				{
					$date_iterate = mktime(date("H"), date("i"), date("s"), $i, date("d"), date("Y"));
					echo "<OPTION value=\"";
					echo date("m", $date_iterate);
					echo "\"";
					if($time_1_parse[month] == date("m", $date_iterate))
					{
						echo " selected='true'";
					};
					echo ">";
					echo date("m", $date_iterate);
					echo "-$i</OPTION>";
					//echo date("m", mktime(date("H"), date("i"), date("s"), $i, date("d"), date("Y")));
				}
	echo "</SELECT>-
	<SELECT name=\"ETA_year$ii\">";

 

However, setting my system clock back to yesterday, 31st of august, i see that the error still occurs, that is : 2 iterations for the date number to change (as shown in the image from the first post). Anymore ideas? :-D

Link to comment
Share on other sites

ok so just so I have the jist of this, only on certaint days it does this... I would say its possible for both, I would double check all your coding, check again if you set your clock back to the regular date/time, then set it back to aug. 31 and it does it.. then it could very welll be a bug, I don't see why its doing that, it could also be something else in your script somewhere else as well. So I would double check that too.

Link to comment
Share on other sites

OK, it's not a bug guys.

 

It looks like the code is trying to determine the month number by creating a date object using todays date and only changing the month. This post was submitted yesterday on Aug. 31, 2008. If you only tried to change the month value you get Feb. 31, 2008 - no such date exists! It would actually evaluate to March 3, 2008! Which is why the select list shows 03-2.

 

I think that code is way over complicated. It doesn't look like you need to do all the date evaluations. All you need to know is the month number of the current month.

echo "</SELECT>-
	<SELECT name=\"ETA_month$ii\">";

	$current_month = date('m');

	for($month=1; $month<=12; $month)
	{
		$selected = ($month == $current_month) ? ' selected="selected"' : '';
		echo "<OPTION value=\"$month\"$selected></option>";
	}

echo "</SELECT>-
	<SELECT name=\"ETA_year$ii\">";

 

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.