input form for date and time php

3 posts by 3 authors in: Forums > CMS Builder
Last Post: July 31, 2013   (RSS)

By nmsinc - July 30, 2013

Anyone have a quick and clean date and time PHP script for a from input including set code?

Thanks  -  nmsinc

nmsinc

By gregThomas - July 31, 2013

Hi nmsincs,

Jerry's suggestion seems the best method, you could also add hour and minute fields to get the time as well:

<?php
$lowestYear = 1980;
$highestYear = 2010;
?>

Month:
<select name="month">
<?php foreach(range(1,12) as $month): ?> 
<option value="<?php echo $month;?>"><?php echo date("F",strtotime("0000-$month"));?></option>
<?php endforeach ?>
</select>


Day:
<select name="day">
<?php foreach(range(1,31)as $day): ?>
<option value="<?php echo $day;?>"><?php echo $day;?></option> 
<?php endforeach ?>
</select>


Year:
<select name="year">
<?php foreach (range($lowestYear,$highestYear) as $year):?>
<option value="<?php echo $year;?>"><?php echo $year;?></option>
<?php endforeach?>
</select>

Hour:
<select name="hour">
<?php foreach (range(0,23) as $hour):?>
<option value="<?php echo $hour;?>"><?php echo $hour;?></option>
<?php endforeach?>
</select>

Minute:
<select name="minute">
<?php foreach (range(0,50) as $minute):?>
<option value="<?php echo $minute;?>"><?php echo $minute;?></option>
<?php endforeach?>
</select>

Then you'd create the date including minutes like this:

<?php $date = date("Y-m-d H:i:s",strtotime(@$_REQUEST['month']."-".@$_REQUEST['day']."-".@$_REQUEST['year']." ".@$_REQUEST['hour'].":".@$_REQUEST['minute'].":00"));  ?>

This is just example code and not suitable for using on a live site, you'd have to validate the values to ensure a user had selected a value for all of the fields.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com