Laravel 4 Form::selectMonth prepend month - php

How can I prepend an option into laravel
{{ Form::selectMonth('month') }}
Result
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
I want to prepend an option value=0 like this
<select class="form-control" name="month">
<option value="0">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
And set the default or selected to the Month placeholder.

You can't, so you'll have to create your own array:
$months = array(0 => 'Month');
foreach (range(1, 12) as $month)
{
$months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
}
pass it to your views:
return View::make('viewname')->with('months', $months);
and use Form::select():
{{ Form::select('month', $months) }}
As a form macro it could be:
Form::macro('selectMonthWithDefault', function($name, $options = array(), $format = '%B')
{
$months = array(0 => 'Month');
foreach (range(1, 12) as $month)
{
$months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
}
return Form::select($name, $months);
});
And don't forget you can also extend the FormBuilder class and create a new Form::selectMonth().

create new file inside app/macros.php and copy paster this line of codes
Form::macro('selectMonths', function($name, $options = array(), $format = '%B')
{
$months = array(0 => 'Month');
foreach (range(1, 12) as $month)
{
$months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
}
return Form::select($name, $months, null, $options);
});
require macros.php in app/start/global.php

Related

Create a dropdown for months using a php function.

I am using this function to create a dropdown for months.
function formMonth(){
$month = strtotime(date('Y').'-'.date('m').'-'.date('j').' - 12 months');
$end = strtotime(date('Y').'-'.date('m').'-'.date('j').' + 0 months');
while($month < $end){
$selected = (date('F', $month)==date('F'))? ' selected' :'';
echo '<option'.$selected.' value="'.date('F', $month).'">'.date('F', $month).'</option>'."\n";
$month = strtotime("+1 month", $month);
}
}
My problem is I need to use numeric values of 1 to 12 for option values.
At this time it use month name for value options like this.
<select size="1" name="month">
<option selected value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
Can anybody tell me how to modify this function.
Thank you.
Try below code:
<select name="month" size='1'>
<?php
for ($i = 0; $i < 12; $i++) {
$time = strtotime(sprintf('%d months', $i));
$label = date('F', $time);
$value = date('n', $time);
echo "<option value='$value'>$label</option>";
}
?>
</select>
If you take a look at the documentation for date(), you'll see under "Format" that you can use n for numeric representation of months, from 1 to 12. (You can also use m for having leading zeroes (01-12)).
This means that you basically only have to change
echo '<option'.$selected.' value="'.date('F', $month).'">'.date('F', $month).'</option>'."\n";
to
echo '<option'.$selected.' value="'.date('n', $month).'">'.date('F', $month).'</option>'."\n";
in your current code, provided that it otherwise works as expected (all I did there was replace date('F', $month) in the values-attribute to date('n', $month).
try this :your problem sloved.
<?php
function formMonth(){
$month = strtotime(date('Y').'-'.date('m').'-'.date('j').' - 12 months');
$end = strtotime(date('Y').'-'.date('m').'-'.date('j').' + 0 months');
$val=1;
echo "<select>";
while($month < $end){
$selected = (date('F', $month)==date('F'))? ' selected' :'';
echo '<option'.$selected.' value='.$val.'>'.date('F', $month).'</option>'."\n";
$month = strtotime("+1 month", $month);
$val++;
}
echo "</select>";
}
formMonth();
?>
Here is your answer:-
<?php
function months($selctedMonth='january'){
$months='<select name="month" size="1">';
for ($i = 12; $i > 0; $i--) {
$time = strtotime(sprintf('-%d months', $i));
$label = date('F', $time);
$selctedM = strtolower($selctedMonth) == strtolower($label) ? 'selected' : '';
$months.="<option value='$label' $selctedM >$label</option>";
}
$months.="</select>";
return $months;
}
echo months();
?>
By default, January will be displayed as selected.
if you write <?php echo months('march'); ?> then march will be selected by default.
<select size="1" name="month">
<option selected value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
If thats what you want.
Enjoy!
<div class="form-group">
<label>Month:</label>
<select id="month" name="month" class="form-control select2" style="width: 100%;">
<option value=""></option>
<?php
$monthArray = range(1, 12);
foreach ($monthArray as $month) {
$monthPadding = str_pad($month, 2, "0", STR_PAD_LEFT);
$fdate = date("F", strtotime("2015-$monthPadding-01"));
echo '<option value="'.$monthPadding.'">'.$fdate.'</option>';
}?>
</select>
</div>
For more deail click here
<select name="month">
<?php
foreach (['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] as $monthNumber => $month) {
echo "<option value='$monthNumber'>{$month}</option>";
}
?>
</select>
I tried some of the answers here, and they have not given me the right solution to display an array of months.
For example, the #Kausha Mehta solution outputted this one.
<select name="month" size="1">
<option value="7">July</option>
<option value="8">August</option>
<option value="10">October</option>
<option value="10">October</option>
<option value="12">December</option>
<option value="12">December</option>
<option value="1">January</option>
<option value="3">March</option>
<option value="3">March</option>
<option value="5">May</option>
<option value="5">May</option>
<option value="7">July</option>
</select>
Which seems to skip some months and duplicating some months.
But this work as expected.
<select id="month" name="month" autocomplete='off'>
<?php
// Create a dropdown for months using a php foreach.
$monthArray = range(1, 12);
//::::
foreach ($monthArray as $month):
$monthCode = str_pad($month, 2, "0", STR_PAD_LEFT);
$selected = ($monthCode === date('m')) ? 'selected' : '';
//All months start with 1
$date = new DateTime(date("Y-").$monthCode."-01");
print '<option '.$selected.' value="'.$monthCode.'">'.$date->format('F').'</option>';
endforeach;
?>
</select>
for($i=1; $i<=12; $i++)
{
echo '<option value="'.date('m', strtotime('2020-'.$i.'-01')).'">'.date('M', strtotime('2020-'.$i.'-01')).'</option>';
}

Searching data with combobox codeigniter

I'm Giovanni,
I got a problem on my code, seems my code work but why the result is unmatch with the values on my combobox..
Can you tell me what's wrong?
Here's my code (on controller. function pencarian_indeks)
function pencarian_indeks() {
//muat library form validation
$this->load->library('form_validation');
if(isset($_POST['submit']))
{
//set aturan validasi untuk setiap field isian
$this->form_validation->set_rules('tanggal', 'Tanggal', 'required');
$this->form_validation->set_rules('kategori', 'Kategori', 'required');
$this->form_validation->set_rules('tahun', 'Tahun', 'required');
$this->form_validation->set_rules('bulan', 'Bulan', 'required');
//cek apakah form validasi berhasil
if ($this->form_validation->run() == FALSE)//jika validasi gagal
{
$this->session->set_flashdata('pesan', validation_errors());
redirect(base_url() . 'indeks');
}
else //jika validasi berhasil
{
$data['kategori'] = $this->input->post('kategori');
$data['tanggal'] = $this->input->post('tanggal');
$data['bulan'] = $this->input->post('bulan');
$data['tahun'] = $this->input->post('tahun');
$this->db->where('DAY(created_at)',$data['tanggal']);
$this->db->where('MONTH(created_at)',$data['bulan']);
$this->db->where('YEAR(created_at)',$data['tahun']);
$this->db->where('kategori', $data['kategori']);
$this->db->from('news');
//Pagination init
$pagination['base_url'] = base_url().'/pencarian/index/page/';
$pagination['total_rows'] = $this->db->count_all_results();
$pagination['full_tag_open'] = "<p><div class=\"pagination\">";
$pagination['full_tag_close'] = "</div></p>";
$pagination['cur_tag_open'] = "<span class=\"current\">";
$pagination['cur_tag_close'] = "</span>";
$pagination['num_tag_open'] = "<span class=\"disabled\">";
$pagination['num_tag_close'] = "</span>";
$pagination['per_page'] = "3";
$pagination['uri_segment'] = 4;
$pagination['num_links'] = 4;
$this->pagination->initialize($pagination);
$data['query'] = $this->news_model->indeks_berita($pagination['per_page'],$this->uri->segment(4,0),$data['tahun'],$data['bulan'],$data['tanggal'],$data['kategori']);
print_r($data['query']);//coba dicek dlu datanya..
exit;
$this->load->vars($data);
$data['hasil5'] = $this->news_model->tampil_populer(2);
$data = array_merge($data,admin_info());
$this->parser->parse('frontend/indeks/indeks',$data);
}
} else {
echo "Hasil Pencarian Tidak Ditemukan!!";
}
}
and on my model (function indeks_berita)
function indeks_berita($perPage, $uri,$tanggal,$bulan,$tahun,$kategori) {
$this->db->select('*');
if($tahun)
$this->db->where('YEAR(created_at)',$tahun);
if($bulan)
$this->db->where('MONTH(created_at)',$bulan);
if($tanggal)
$this->db->where('DAY(created_at)',$tanggal);
if($kategori)
$this->db->where('kategori', $kategori);
$this->db->order_by('id', 'asc');
$getData = $this->db->get('news', $perPage, $uri);
print_r ($this->db->last_query());
//print_r ($getData->result_array());
exit();
if ($getData->num_rows() > 0){
return $getData->result_array();
}else{
return null;
}
}
on my view (the comboboxes)
<p>Lihat Arsip Tanggal :
<select name="kategori">
<option value="" selected>Pilih kategori</option>
<option value="news">News</option>
<option value="bandung_city">Bandung City</option>
<option value="hot_gossip">Hot Gossip</option>
<option value="sainstek">Sains & Tek</option>
<option value="kriminal">Kriminalitas</option>
<option value="persibnews">#PERSIBONTHENEWS</option>
</select>
<select name="tahun">
<option value="" selected>Pilih Tahun</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
</select>
<select name="bulan">
<option value="" selected>Pilih Bulan</option>
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Maret</option>
<option value="4">April</option>
<option value="5">Mei</option>
<option value="6">Juni</option>
<option value="7">Juli</option>
<option value="8">Agustus</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">Desember</option>
</select>
<select name="tanggal">
<option value="" selected>Pilih</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<input type="submit" value="Cari!" name="cari" id="cari"/>
And here's the output query when I submitted that code :
SELECT * FROM (`news`) WHERE YEAR(created_at) = '2' AND MONTH(created_at) = '17' AND DAY(created_at) = 'bandung_city' AND `kategori` = '2014' ORDER BY `id` asc LIMIT 3
(seems not right)
Thank you, I'm glad if you help me to find what's wrong
Your form action seems to be incorrect you do not have any indeks method inside indeks contorller.
Change is as below
<form action="<?php echo base_url(); ?>indeks/pencarian_indeks" method="post">
It seems like that passing the order of the arguments to your model function is wrong.
Instead of this
$data['query'] = $this->news_model->indeks_berita(
$pagination['per_page'],
$this->uri->segment(4,0),
$data['tahun'], // it seems that model function accepts tangall as 3rd arg
$data['bulan'],
$data['tanggal'],// it seems that model function accepts tahun as 5th arg
$data['kategori']
);
Try
$data['query'] = $this->news_model->indeks_berita(
$pagination['per_page'],
$this->uri->segment(4,0),
$data['tanggal'],
$data['bulan'],
$data['tahun'],
$data['kategori']
);

How to create a dropdown of time?

I want to create a dropdown for time in PHP. It should be of 24 hour format with an interval of 30 minutes like 00:00 then 00:30 then 13:00 and so on.I want it round the clock.
I have used this code
<?php
$start = strtotime('12:00 AM');
$end = strtotime('11:59 PM');
?>
<select style="width:85px;" name="select1" id="select1">
<?php for($i = $start;$i<=$end;$i+=1800){ ?>
<option value='<?php echo date('G:i', $i); ?>'><?php echo date('G:i', $i); ?></option>;
<?php } ?>
</select>
The problem is that it doesn't cover the last span of 11:30 to 12:00 AM as the increment value exceeds the end, Is there a direct way to do this in PHP?
Uhm, it is extremely unlikely that in the future, our clocks suddenly gain a 25th hour, so a normal loop should do fine:
for($hours=0; $hours<24; $hours++) // the interval for hours is '1'
for($mins=0; $mins<60; $mins+=30) // the interval for mins is '30'
echo '<option>'.str_pad($hours,2,'0',STR_PAD_LEFT).':'
.str_pad($mins,2,'0',STR_PAD_LEFT).'</option>';
You could use that function to create time list anywhere on your page:
/**
*
* Get times as option-list.
*
* #return string List of times
*/
function get_times ($default = '19:00', $interval = '+30 minutes') {
$output = '';
$current = strtotime('00:00');
$end = strtotime('23:59');
while ($current <= $end) {
$time = date('H:i', $current);
$sel = ($time == $default) ? ' selected' : '';
$output .= "<option value=\"{$time}\"{$sel}>" . date('h.i A', $current) .'</option>';
$current = strtotime($interval, $current);
}
return $output;
}
Get times:
<select name="time"><?php echo get_times(); ?></select>
Here is the result:
<select name="time">
<option value="00:00">12.00 AM</option>
<option value="00:30">12.30 AM</option>
<option value="01:00">01.00 AM</option>
<option value="01:30">01.30 AM</option>
<option value="02:00">02.00 AM</option>
<option value="02:30">02.30 AM</option>
<option value="03:00">03.00 AM</option>
<option value="03:30">03.30 AM</option>
<option value="04:00">04.00 AM</option>
<option value="04:30">04.30 AM</option>
<option value="05:00">05.00 AM</option>
<option value="05:30">05.30 AM</option>
<option value="06:00">06.00 AM</option>
<option value="06:30">06.30 AM</option>
<option value="07:00">07.00 AM</option>
<option value="07:30">07.30 AM</option>
<option value="08:00">08.00 AM</option>
<option value="08:30">08.30 AM</option>
<option value="09:00">09.00 AM</option>
<option value="09:30">09.30 AM</option>
<option value="10:00">10.00 AM</option>
<option value="10:30">10.30 AM</option>
<option value="11:00">11.00 AM</option>
<option value="11:30">11.30 AM</option>
<option value="12:00">12.00 PM</option>
<option value="12:30">12.30 PM</option>
<option value="13:00">01.00 PM</option>
<option value="13:30">01.30 PM</option>
<option value="14:00">02.00 PM</option>
<option value="14:30">02.30 PM</option>
<option value="15:00">03.00 PM</option>
<option value="15:30">03.30 PM</option>
<option value="16:00">04.00 PM</option>
<option value="16:30">04.30 PM</option>
<option value="17:00">05.00 PM</option>
<option value="17:30">05.30 PM</option>
<option value="18:00">06.00 PM</option>
<option value="18:30">06.30 PM</option>
<option value="19:00" selected="">07.00 PM</option>
<option value="19:30">07.30 PM</option>
<option value="20:00">08.00 PM</option>
<option value="20:30">08.30 PM</option>
<option value="21:00">09.00 PM</option>
<option value="21:30">09.30 PM</option>
<option value="22:00">10.00 PM</option>
<option value="22:30">10.30 PM</option>
<option value="23:00">11.00 PM</option>
<option value="23:30">11.30 PM</option>
</select>
Why not use the DateTime object which is specifically designed for this type of thing?
$starttime = '00:00:00';
$time = new DateTime($starttime);
$interval = new DateInterval('PT30M');
$temptime = $time->format('H:i:s');
do {
echo $temptime . '<br />';
$time->add($interval);
$temptime = $time->format('H:i:s');
} while ($temptime !== $starttime);
$hoursArray = range(0, 12);
$minutesArray = range(0, 60);
$amPmArray = array('AM', 'PM');
//Pad the min and hr fields with a leading zero
array_walk($hoursArray, 'padLeadingZero');
array_walk($minutesArray, 'padLeadingZero');
/**
* Pad give input with a leading 0
* #param int $val Value that needs to be padded with a leading 0
*/
function padLeadingZero(&$val){
$val = str_pad($val, $padLength = 2, $padString = '0', $padType = STR_PAD_LEFT);
}
for joomla 2.* user
<field
name="time"
type="list"
required="true"
label="Time"
description="Time"
class="input-medium validate-time"
>
<option value="00:00:00">00:00</option>
<option value="00:30:00">00:30</option>
<option value="01:00:00">01:00</option>
<option value="01:30:00">01:30</option>
<option value="02:00:00">02:00</option>
<option value="02:30:00">02:30</option>
<option value="03:00:00">03:00</option>
<option value="03:30:00">03:30</option>
<option value="04:00:00">04:00</option>
<option value="04:30:00">04:30</option>
<option value="05:00:00">05:00</option>
<option value="05:30:00">05:30</option>
<option value="06:00:00">06:00</option>
<option value="06:30:00">06:30</option>
<option value="07:00:00">07:00</option>
<option value="07:30:00">07:30</option>
<option value="08:00:00">08:00</option>
<option value="08:30:00">08:30</option>
<option value="09:00:00">09:00</option>
<option value="09:30:00">09:30</option>
<option value="10:00:00">10:00</option>
<option value="10:30:00">10:30</option>
<option value="11:00:00">11:00</option>
<option value="11:30:00">11:30</option>
<option value="12:00:00">12:00</option>
<option value="12:30:00">12:30</option>
<option value="13:00:00">13:00</option>
<option value="13:30:00">13:30</option>
<option value="14:00:00">14:00</option>
<option value="14:30:00">14:30</option>
<option value="15:00:00">15:00</option>
<option value="15:30:00">15:30</option>
<option value="16:00:00">16:00</option>
<option value="16:30:00">16:30</option>
<option value="17:00:00">17:00</option>
<option value="17:30:00">17:30</option>
<option value="18:00:00">18:00</option>
<option value="18:30:00">18:30</option>
<option value="19:00:00">19:00</option>
<option value="19:30:00">19:30</option>
<option value="20:00:00">20:00</option>
<option value="20:30:00">20:30</option>
<option value="21:00:00">21:00</option>
<option value="21:30:00">21:30</option>
<option value="22:00:00">22:00</option>
<option value="22:30:00">22:30</option>
<option value="23:00:00">23:00</option>
<option value="23:30:00">23:30</option>
</field>
Convert your time into hours first and then by using for loop you can generate the list of time like you want.
$s_temp = new DateTime('17:00');
$stime = $s_temp->format('H');
$e_temp = new DateTime('03:30');
$etime = $e_temp->format('H');
$data = array();
if starting time is < 12
if($stime < 12){
for($hours=$stime; $hours<12; $hours++){
for($mins=0; $mins<60; $mins+=30){
$data[] = str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).' am';
}
}
for($hours=0; $hours<=$etime; $hours++){
for($mins=0; $mins<60; $mins+=30){
$data[] = str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).' pm';
}
}
}
if starting time is > 12
if($stime > 12){
for($hours=$stime; $hours<24; $hours++){
for($mins=0; $mins<60; $mins+=30){
$data[] = str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).' pm';
}
}
for($hours=0; $hours<=$etime; $hours++){
for($mins=0; $mins<60; $mins+=30){
$data[] = str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).' am';
}
}
}
return $data;
Variation of Christian's answer using AM and PM:
for ($hours=0; $hours<24; $hours++) { // the interval for hours is '1'
for($mins=0; $mins<60; $mins+=30) { // the interval for mins is '30'
$thehour = str_pad($hours,2,'0',STR_PAD_LEFT);
if ($thehour == "00") {
$thehour = "12";
}
if ($thehour > "12") {
$thehour = $thehour - 12;
if ($thehour < 10) {
$thehour = "0" . $thehour;
}
}
$theminutes = str_pad($mins,2,'0',STR_PAD_LEFT);
$mytime = $thehour.":".$theminutes;
if ($hours < 12) {
$mytime = $mytime . " AM";
}
else {
$mytime = $mytime . " PM";
}
echo "<option>".$mytime."</option>";
}
}

php function to list months for dropdown

I have this function
function bookingMonthField() {
$str="";
for($i = 0; $i < 16; $i++) {
$time = mktime(0, 0, 0, date('n') + $i);
$str .="<option value=" . date('Yn', $time) . ">" . date('M Y', $time) . "</option>";
}
return $str;
}
Which works (almost) as it should, except it is returning no Feb and 2x March:
<option value="20119">Sep 2011</option>
<option value="201110">Oct 2011</option>
<option value="201111">Nov 2011</option>
<option value="201112">Dec 2011</option>
<option value="20121">Jan 2012</option>
<option value="20123">Mar 2012</option>
<option value="20123">Mar 2012</option>
<option value="20124">Apr 2012</option>
<option value="20125">May 2012</option>
<option value="20126">Jun 2012</option>
<option value="20127">Jul 2012</option>
<option value="20128">Aug 2012</option>
<option value="20129">Sep 2012</option>
<option value="201210">Oct 2012</option>
<option value="201211">Nov 2012</option>
<option value="201212">Dec 2012</option>
ANy ideas why this is happening?
Just tell your script, you want the first of the month:
$time = mktime(0, 0, 0, date('n') + $i, 1);
Or wait another day, then your website is automatically fixed :)
mktime will use the current day if none is provided. Today is the 29th, so February is skipped. Instead, specify "1" for the day.

Validation of Specific Date using Php or Javascript

I need a quick way to validate in php or javascript whether a date is an invalid date such as (2/31 or 04/31 - since they dont exist), ALONG with if it is a LEAP year or not.
My code:
<?php
$month = $_POST["mo"];
$day = $_POST["theDay"];
$yr = $_POST["year"];
if (!isset($_POST['submit'])) { // if page is not submitted; echo form
?>
<h2 style="margin:0; padding:0;">Date Selection</h2>
<span style="color:gray; font-size:0.8em;">By Joseph Dickinson</span>
<FORM method="POST" action="<?php echo $PHP_SELF;?>">
<select name="mo">
<option disabled="disabled">SELECT MONTH</option>
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
<select name="theDay">
<option disabled="disabled">SELECT DAY</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
<select name="year">
<option disabled="disabled">SELECT YEAR</option>
<option>2011</option>
<option>2010</option>
<option>2009</option>
<option>2008</option>
<option>2007</option>
<option>2006</option>
<option>2005</option>
<option>2004</option>
<option>2003</option>
<option>2002</option>
<option>2001</option>
<option>2000</option>
<option>1999</option>
<option>1998</option>
<option>1997</option>
<option>1996</option>
<option>1995</option>
<option>1994</option>
<option>1993</option>
<option>1992</option>
<option>1991</option>
<option>1990</option>
<option>1989</option>
<option>1988</option>
<option>1987</option>
<option>1986</option>
<option>1985</option>
<option>1984</option>
<option>1983</option>
<option>1982</option>
<option>1981</option>
<option>1982</option>
</select>
<INPUT TYPE="submit" value="Send" name="submit" />
</FORM>
<?
} else {
echo "You chose: " . " ". $month . " ".$day . ", ". $yr;
}
?>
If someone could please help me out here.
First, add numbers as values for months:
<select name="mo">
<option disabled="disabled">SELECT MONTH</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
Then use checkdate():
<?php
$month = $_POST["mo"];
$day = $_POST["theDay"];
$yr = $_POST["year"];
if (checkdate($month, $day, $yr) !== true) {
// ?
}
?>
And if you want to do it in the browser as well, first add an onsubmit handler to the year form:
<FORM method="POST" action="<?php echo $PHP_SELF;?>" onsubmit="return checkLeap(this);">
Then your function:
function checkLeap(form) {
var month = form.mo.options[form.mo.selectedIndex].value;
var day = form.theDay.options[form.theDay.selectedIndex].value;
var year = form.year.options[form.year.selectedIndex].value;
if (month == 2 && day == 29 && (new Date(year,month,year).getDate() != 29)) {
alert('That is not a leap year. Please fix.');
return false;
}
return true;
}
When you create a timestamp with mktime() with a date too high for the month, it just skips up to the next month, with a date of the difference. For example, Feb 31 produces Mar 3. So to make sure it's valid, just create the time, and then re-check that the month hasn't changed.
$month = 2;
$date = 31;
$year = 2011;
$time = mktime(1, 0, 0, $month, $date, $year);
if (date('n', $time) != $month) {
// invalid error!
}
The same mechanism also automatically accounts for leap years. But if you want to know if it's a leap year or not, just check with date('L', $time);.
http://php.net/date
http://php.net/mktime
As a side note, if you do validate it with Javascript, you must also check with PHP. Never rely on client-side validation.

Categories