gorilla1
10-20-2002, 05:49 PM
The code below comes from a snippet on evolt.org. I am trying to use it so that the viewer can select a date which will be used to archive a file. Once I get the basic form processing to work, the form will be set up so that a default value (based on the modification time of the file) will be used. However, when I run this, the date turns up as 0-0-0, so it appears that the processing of the posted data is failing, for reasons that I cannot see. Can anyone spot the error in the logic?
G
<?
if($HTTP_POST_VARS['submit']) {
// Get date selected and write it to display
$PostedDate = GetDateSelectString();
echo $PostedDate;
echo "<br>";
echo strtotime ($PostedDate), "\n";
}
// funtion below writes the select and option tags
// ispost is set to false only if you want to use get instead of post
// $Prefix is used only when for multiple date fields on a form, such as begin and end date
function WriteDateSelect($BeginYear = 0,
$EndYear = 0,
$IsPosted = true,
$Prefix = '')
{
if (! $BeginYear)
{
$BeginYear = date('Y');
}
if (! $EndYear)
{
$EndYear = $BeginYear;
}
$Year = $IsPosted
? (int) $_POST[$Prefix . 'Year']
: (int) $_GET[$Prefix . 'Year'];
$Month = $IsPosted
? (int) $_POST[$Prefix . 'Month']
: (int) $_GET[$Prefix . 'Month'];
$Day = $IsPosted
? (int) $_POST[$Prefix . 'Day']
: (int) $_GET[$Prefix . 'Day'];
echo '<select name="', $Prefix, 'Year">
';
for ($i = $BeginYear; $i <= $EndYear; $i++)
{
echo '<option ';
if ($i == $Year)
echo 'selected="yes"';
echo '>', $i, '</option>
';
}
echo '</select>-
<select name="', $Prefix, 'Month">
';
for ($i = 1; $i <= 12; $i++)
{
echo '<option ';
if ($i == $Month)
echo 'selected="yes"';
echo '>', $i, '</option>
';
}
echo '</select>-
<select name="', $Prefix, 'Day">
';
for ($i = 1; $i <= 31; $i++)
{
echo '<option ';
if ($i == $Day)
echo 'selected="yes"';
echo '>', $i, '</option>
';
}
echo '</select>
';
return;
}
// process the data and inject slashes into date
function GetDateSelectString($IsPosted = true,
$Prefix = '')
{
if ($IsPosted)
{
return (int) $_POST[$Prefix . 'Year']
. '-' . (int) $_POST[$Prefix . 'Month']
. '-' . (int) $_POST[$Prefix . 'Day'];
}
return (int) $_GET[$Prefix . 'Year']
. '-' . (int) $_GET[$Prefix . 'Month']
. '-' . (int) $_GET[$Prefix . 'Day'];
}
?>
<center>
<table border="0" cellpadding="1" cellspacing="0" width="380" >
<tr class="frm1">
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr class="frm1">
<td class="frm1" width="100%" height="35"><p class="frm1">Manage Files</p></td>
</tr>
<tr>
<td class="frm2" colspan="2">
<p class="frm2">Move directory:</p><form name="addEntry" action="<?=$PHP_SELF?>" method="post">
<table border="0" cellspacing="0" cellpadding="4">
<?php $mod = filemtime("xfile"); ?>
<tr><td class="frm2"><p class="frm2">Rename to:</p></td></tr><tr><td align="left"> <?php WriteDateSelect(2002, 2005); ?></td></tr>
<tr><td><IMG src="../1pix.gif" alt="" BORDER=0></td></tr>
<tr><td align="center" >
<input type="submit" name="submit" class="button" value="Rename it">
</td></tr>
</table></form>
</td>
</tr>
</table></center>
G
<?
if($HTTP_POST_VARS['submit']) {
// Get date selected and write it to display
$PostedDate = GetDateSelectString();
echo $PostedDate;
echo "<br>";
echo strtotime ($PostedDate), "\n";
}
// funtion below writes the select and option tags
// ispost is set to false only if you want to use get instead of post
// $Prefix is used only when for multiple date fields on a form, such as begin and end date
function WriteDateSelect($BeginYear = 0,
$EndYear = 0,
$IsPosted = true,
$Prefix = '')
{
if (! $BeginYear)
{
$BeginYear = date('Y');
}
if (! $EndYear)
{
$EndYear = $BeginYear;
}
$Year = $IsPosted
? (int) $_POST[$Prefix . 'Year']
: (int) $_GET[$Prefix . 'Year'];
$Month = $IsPosted
? (int) $_POST[$Prefix . 'Month']
: (int) $_GET[$Prefix . 'Month'];
$Day = $IsPosted
? (int) $_POST[$Prefix . 'Day']
: (int) $_GET[$Prefix . 'Day'];
echo '<select name="', $Prefix, 'Year">
';
for ($i = $BeginYear; $i <= $EndYear; $i++)
{
echo '<option ';
if ($i == $Year)
echo 'selected="yes"';
echo '>', $i, '</option>
';
}
echo '</select>-
<select name="', $Prefix, 'Month">
';
for ($i = 1; $i <= 12; $i++)
{
echo '<option ';
if ($i == $Month)
echo 'selected="yes"';
echo '>', $i, '</option>
';
}
echo '</select>-
<select name="', $Prefix, 'Day">
';
for ($i = 1; $i <= 31; $i++)
{
echo '<option ';
if ($i == $Day)
echo 'selected="yes"';
echo '>', $i, '</option>
';
}
echo '</select>
';
return;
}
// process the data and inject slashes into date
function GetDateSelectString($IsPosted = true,
$Prefix = '')
{
if ($IsPosted)
{
return (int) $_POST[$Prefix . 'Year']
. '-' . (int) $_POST[$Prefix . 'Month']
. '-' . (int) $_POST[$Prefix . 'Day'];
}
return (int) $_GET[$Prefix . 'Year']
. '-' . (int) $_GET[$Prefix . 'Month']
. '-' . (int) $_GET[$Prefix . 'Day'];
}
?>
<center>
<table border="0" cellpadding="1" cellspacing="0" width="380" >
<tr class="frm1">
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr class="frm1">
<td class="frm1" width="100%" height="35"><p class="frm1">Manage Files</p></td>
</tr>
<tr>
<td class="frm2" colspan="2">
<p class="frm2">Move directory:</p><form name="addEntry" action="<?=$PHP_SELF?>" method="post">
<table border="0" cellspacing="0" cellpadding="4">
<?php $mod = filemtime("xfile"); ?>
<tr><td class="frm2"><p class="frm2">Rename to:</p></td></tr><tr><td align="left"> <?php WriteDateSelect(2002, 2005); ?></td></tr>
<tr><td><IMG src="../1pix.gif" alt="" BORDER=0></td></tr>
<tr><td align="center" >
<input type="submit" name="submit" class="button" value="Rename it">
</td></tr>
</table></form>
</td>
</tr>
</table></center>