Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing number with current date? 1

Status
Not open for further replies.

Tasuki

MIS
Jul 26, 2002
169
US
I'm trying to create a query that searches between dates. There is the begin date fields which are easy, I just set to the earliest date that the records will go, in this case January 1, 1998. But for the end date, I want it to preselect the current month and current date.

It works fine, except the month is not displaying the current month. Here is the code I came up with... I have a feeling it may be a problem where it compares the current month. Why wouldn't it work since I'm pulling out only the month value to use to compare?

Code:
<?php

$yr = 1998;
$c_yr=date('Y');

$slctbx = '';

while ($yr <= $c_yr) {
  if($yr == $c_yr) {
    $slctbx .= "<option value=\"".$yr."\" SELECTED>".$yr."</option>\n";
    $yr++;
  } else {
    $slctbx .= "<option value=\"".$yr."\">".$yr."</option>\n";
    $yr++;
  }
}

$mth = 01;
$emth = 12;
$cmth=date('m');

$slctbx2 = '';

while ($mth <= $emth) {
  if($mth == $cmth) {
    $slctbx2 .= "<option value=\"".$mth."\" SELECTED>".$mth."</option>\n";
    $mth++;
  } else {
    $slctbx2 .= "<option value=\"".$mth."\">".$mth."</option>\n";
    $mth++;
  }
}

echo "Month:<select name='month'>$slctbx2</select>Year:<select name='yr'>$slctbx</select>";
?>

Any ideas or suggestions is greatly appreciated.

Thanks,

-T
 
The reason why I set the ending date to preselect latest month and year is in case the user decides not to query by date, it will search between the earliest recorded record to the latest so all records will be shown if they decide to submit without selecting anything.
 
Your code worked fine for me, but I took the liberty of tightening it up a little... :)
Code:
<?
$c_yr=date('Y');

$slctbx = '';

for ($yr=1998;$yr<$c_yr;$yr++)
    $slctbx .= '<option value="' . $yr . '">' . $yr . "</option>\n";
$slctbx .= '<option value="' . $yr . '" SELECTED>' . $yr . "</option>\n";

$cmth=date('n');

$slctbx2 = '';

for ($mth=1;$mth<= 12;$mth++) {
  if($mth == $cmth)
    $slctbx2 .= '<option value="' . $mth . '" SELECTED>' . $mth . "</option>\n";
 else $slctbx2 .= '<option value="' . $mth . '">' . $mth . "</option>\n";
  }

echo 'Month:<select name="month">' . $slctbx2 . '</select>Year:<select name="yr">' . $slctbx . '</select>';
?>

I changed the double quotes to single quotes, thereby eliminating the need to escape the double quotes in the strings. I think it gives cleaner looking code.

I also replaced the "while" statments with "for" which makes the code more understandable (IMHO). You also can remove the initialization of the variables used in the loops, since the for statement includes initialization.

Ken
 
Thank you Ken.

It is strange that my use of -
Code:
$cmth=date('m');
did not work, while -
Code:
$cmth=date('n');
worked fine. What's the difference?

Anyway, thanks for your help and cleaning up my code. :)

-T
 
Hmm..

After some more testing... it seems to work with the 'm' now too. Well, thanks all the same.
 
Final addition if anyone may be looking.. added some leading zeros to format the month to look better in the select box.

Code:
<?
$c_yr=date('Y');

$slctbx = '';

for ($yr=1998;$yr<$c_yr;$yr++)
    $slctbx .= '<option value="' . $yr . '">' . $yr . "</option>\n";
$slctbx .= '<option value="' . $yr . '" SELECTED>' . $yr . "</option>\n";

$cmth=date('n');

$slctbx2 = '';

for ($mth=1;$mth<= 12;$mth++) {
  if($mth == $cmth)
    $slctbx2 .= '<option value="' . $mth . '" SELECTED>' . str_pad($mth, 2, '0', STR_PAD_LEFT) . "</option>\n";
 else $slctbx2 .= '<option value="' . $mth . '">' . str_pad($mth, 2, '0', STR_PAD_LEFT) . "</option>\n";
  }

echo 'Month:<select name="month">' . $slctbx2 . '</select>Year:<select name="yr">' . $slctbx . '</select>';
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top