我在网上找到了以下关于定义日历下拉列表的PHP函数(以月/日/年格式)。该功能运行良好,但我希望:
a) 保留Month、Day和Year的选定值,以便能够轻松编辑值(值通过foreach循环动态定义),以及
b) 我想在多个变量上重复使用该函数(到目前为止,我正在使用它来定义出生日期,但我也想在其他字段上使用相同的函数)。
Month、Day和Year的值是动态定义的,这一事实让我有点困惑,当然我还不熟悉函数定义。如有任何帮助,我们将不胜感激!
以下是功能:
function make_calendar_pulldowns() {
// Make the months array:
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
// Make the months pull-down menu:
//echo '<p><label for="dob" style="font-family: Verdana, Arial; font-size: 1.0em; font-weight: 600; color: #595959; line-height: 1.9em;">Date of Birth</label></p>';
echo '<select required="required" name="month" id="month" style="width: 33%; display: inline; float: left; margin-left: 1%; margin-right: 0%">';
echo '<option selected value="">Month</option>n';
foreach ($months as $key => $value) {
echo "<option value="$key" >$value</option>n";
}
echo '</select>';
// Make the days pull-down menu:
echo '<select required="required" name="day" id="day" style="width: 33%; display: inline; float: left; margin-left: 0%; margin-right: 0%" ;>';
echo '<option selected value="">Day</option>n';
for ($day = 1; $day <= 31; $day++) {
echo "<option value="$day">$day</option>n";
}
echo '</select>';
// Make the years pull-down menu:
echo '<select required="required" name="year" id="year" style="width: 33%; display: inline; float: left; margin-left: 0%; margin-right: 0%" ;>';
echo '<option selected value="">Year</option>n';
for ($year = 1910; $year <= 2000; $year++) {
echo "<option value="$year">$year</option>n";
}
echo '</select>';
一旦定义了值,我就用以下代码将它们转换为MySQL格式:
// Validate the month.
if (is_numeric ($month)) {
$dob = $month . '-';
} else {
$action['result'] = 'error';
array_push($text,'Please insert a valid Month for patient birth date');
}
// Validate the day.
if (is_numeric ($day)) {
$dob .= $day . '-';
} else {
$action['result'] = 'error';
array_push($text,'Please insert a valid Day for patient birth date');
}
// Validate the year.
if (is_numeric ($year)) {
$dob = $year . '-' . $month . '-' . $day; // Set Birthdate in SQL format
最后,我用以下代码调用表单中的函数:
<!-- <p><label for="dob">Date of Birth <img src="../img/req.gif" alt="Required"</label></p>
<?php make_calendar_pulldowns(); ?> -->
实际上它的某些部分有点偏离:
// Make the months array:
$months = array ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
// some months are a bit off, the keys are missing
// Should be something like this:
$months = array ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$month_num = range(1, 12);
$months = array_combine($month_num, $months);
日子是硬编码的,2月31日怎么样?
我想《岁月》应该还可以。
为什么不使用日期选择器(特别是jQuery UI)来实现此目的呢?
$(function() {
$( "#datepicker" ).datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'yy-mm-dd', // iso format
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<form method="POST">
<p>Date: <input type="text" id="datepicker" name="date"></p>
<input type="submit" name="submit1" />
</form>
然后在PHP中,只需像普通的$_POST
一样调用它
if(isset($_POST['submit1'])) {
$date = $_POST['date'];
}
更多API信息:http://jqueryui.com/datepicker/