date problem...check this out..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pleigh
Forum Contributor
Posts: 445
Joined: Wed Jan 19, 2005 4:26 am

date problem...check this out..

Post by pleigh »

i have a code here for the date

Code: Select all

if (empty($_POST['month']) || empty($_POST['day']) || empty($_POST['year']))
    {
        $sd = false;
        $message .= 'You forgot to enter the project start date<br>';
    }
    else
    {
        $sd = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
    }
	
	//check for poject duration end date
	if (empty($_POST['month']) || empty($_POST['day']) || empty($_POST['year']))
    {
        $ed = false;
        $message .= 'You forgot to enter the project end date<br>';
    }
    else
    {
        $ed = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
    }

if ($sd && $ed)
{
    $query = "INSERT INTO table(startdate, enddate)
    VALUES('$sd','$ed');
    $result = @mysql_query($query);
    if ($result)
		{
			echo 'project written to database';
			echo '<meta http-equiv="refresh" content="3;url=http://localhost/mysample/project.php">';
			exit();
		}
		else
		{
			echo 'no data was written to the database';
		}
		mysql_close();
		
	}
	else
	{
		$message .= 'Please try again';
	}//end connection to db
then on the form

Code: Select all

&lt;table width=&quote;100%&quote; cellpadding=&quote;1&quote; cellspacing=&quote;1&quote;&gt;
						&lt;tr&gt;
							&lt;td width=&quote;20%&quote;&gt;&lt;b/&gt;Start Date&lt;/td&gt;
							&lt;td width=&quote;80%&quote;&gt;&lt;? calendar1(); ?&gt;&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
							&lt;td width=&quote;20%&quote;&gt;&lt;b/&gt;End Date&lt;/td&gt;
							&lt;td width=&quote;80%&quote;&gt;&lt;? calendar2(); ?&gt;&lt;/td&gt;
						&lt;/tr&gt;					
					&lt;/table&gt;
i have 2 calendar functions:
calendar1() and calendar2()
which has an identical code below

Code: Select all

function calendar1()
{
	//months array
	$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	
	//dropdown
	echo '<select name="month">';
	echo "<option value=\"\">Month</option><br>";
	foreach ($months as $key => $value) 
	{
		echo "<option value=\"$key\">$value</option>\n";
	}
	echo '</select>
	<select name="day">';
	echo "<option value=\"\">Day</option><br>";
	for ($day = 1; $day <= 31; $day++) 
	{
		echo "<option value=\"$day\">$day</option>\n";
	}
	echo '</select>
	<select name="year">';
	echo "<option value=\"\">Year</option><br>";
	$year = 2005;
	while ($year <= 2010) 
	{
		echo "<option value=\"$year\">$year</option>\n";
		$year++;
	}
	echo '</select>';
}
the problem now is that whenever i supply the both with different dates, in the database, the end date is reported in the database...for example, if i select July 13 2005 from startdate and July 14 2006, when i open mysql, July 14 2005 is written to both the startdate and enddate fields....another problem is that whenever i leave the startdate blank and supply some data to enddate, again, the enddate is written to the database to both fields, instead of prompting me that i should supply some data to the startdate...but if i did this another way around(leaving enddate blank and supplying some data to startdate), the desired error will be seen...

please help..thanks in advance..
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You might want to take a look at lines 24 to 30 in that first script ;)
Post Reply