PHP display 01 instead of 1 [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Formatting a number with leading zeros in PHP
I am populating a select box with php.
This is the code:
$select_month_control = '<select name="month" id="month">';
for($x = 1; $x <= 12; $x++) {
$select_month_control.= '<option value="'.$x.'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>';
This is creating this:
<select id="month" 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 selected="selected" value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
The problem is that I need the 1, 2, 3 etc to be 01, 02, 03 etc... Like this:
<option value="01">January</option>
instead of:
<option value="1">January</option>
How can I do this?

You can use sprintf("%02d", $number) to format strings.
Edit: See http://www.php.net/manual/en/function.sprintf.php for more information about format strings

user str_pad ref : http://php.net/manual/en/function.str-pad.php
$select_month_control = '<select name="month" id="month">';
for($x = 1; $x <= 12; $x++) {
$select_month_control.= '<option value="'.str_pad($x, 2, "0", STR_PAD_LEFT).'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>';

You might want to try using the str_pad() function within your loop :
str_pad — Pad a string to a certain length with another string
for($x = 1; $x <= 12; $x++) {
$value = str_pad($x,2,"0",STR_PAD_LEFT);
$select_month_control.= '<option value="'.$value.'">'.$value.'</option>';
}

Related

PHP - How do I show the correct selected <option> value?

I'm writing the html codes in php file.
I have a dropdown to allow user query results with month & year that looks like this.
The query search on default current month and year, but for example when I search for April 2018, the results are be correct , but the dropdown selectoption still display July 2018.
My codes:
<table width="80%" border="0" align="center" cellpadding="7" cellspacing="2">
<tr bgcolor="#ffffff">
<td height="6" colspan="2" bgcolor="#E6E6FA">
Month
<select name="soutlet" id="soutlet">
<option selected="selected" value="7">July</option>
<option value="">-----------</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>
Year
<select name="soutletto" id="soutletto">
<option selected="selected" value="2018">2018</option>
<option value="">-----</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
</select>
<input type="submit" name="btn_search" id="btn_search" value="Search" ></td>
</tr>
How do I modify my html codes in order the dropdown showing the correct month and year? If I search for April 2018 it will shows
This is a little easier if you create your <select> using an array and a loop, ie.
<?php
$months = array(
1=>"January",
2=>"February",
3=>"March",
4=>"April",
5=>"May",
6=>"June",
7=>"July",
8=>"August",
9=>"September",
10=>"October",
11=>"November",
12=>"December"
);
?>
<select name="soutlet" id="soutlet">
<option value="">-----------</option>
<?php foreach($months as $key=>$month){
//if posted value matches current value add selected
$sel = ($key==$_POST['soutlet']) ? ' selected="selected"' : '';
echo '<option value="'.$key.'" '.$sel.'>'.$month.'</option>';
}
?>
</select>
you should add selected attribute when $_post['month'] = option value
$_post for post requests and $_get for get requests
Please create function for both year / month print and set function argument based on that
function month_select($month = '', $name = 'soutlet', $class = 'soutlet', $id = 'soutlet')
{
$month_sel = '<select id="'.$id.'" name="'.$name.'" name="'.$class.'" >';
$month_sel .= '<option value="">Select Month</option>';
for($i = 1; $i <= 12; $i++)
{
$month_sel .= '<option value="'.$i.'" '.(($month == $i)?'selected="selected"':'').'>'.date("F", mktime(0, 0, 0, $i, 10)).'</option>';
}
$month_sel .= '</select>';
return $month_sel;
}
function year_select($year = '', $name = 'soutletto', $class = 'soutletto', $id = 'soutletto')
{
$year_sel = '<select id="'.$id.'" name="'.$name.'" name="'.$class.'" >';
$year_sel .= '<option value="">Select Year</option>';
for($i = 0; $i <= 5; $i++)
{
$cur_year = date("Y") - $i;
$year_sel .= '<option value="'.$cur_year.'" '.(($cur_year == $year)?'selected="selected"':'').'>'.$cur_year.'</option>';
}
$year_sel .= '</select>';
return $year_sel;
}
echo month_select($_GET['soutlet']);
echo year_select($_GET['soutletto']);
OR
You can use POST method
echo month_select($_POST['soutlet']);
echo year_select($_POST['soutletto']);
To solve this problem you can store month value into a variable while submitting the form. Like $month = $_POST['soutlet'] if you use POST method (do some validation ).
Then you have to compare this value inside the html select > option and set it to be selected. Like this inside <option> tag
<option value="1" <?php echo isset($month) ? ($month == '1' ? 'selected' : '') : ''; ?> >January</option>
And don't forget to remove your statically defined selected from <option> use the same logic for the year.
Assuming you are posting to the same page, you can do something like this. (simplified for only 1 month and 1 year, but you get the idea)
table width="80%" border="0" align="center" cellpadding="7" cellspacing="2">
<tr bgcolor="#ffffff">
<td height="6" colspan="2" bgcolor="#E6E6FA">
Month
<select name="soutlet" id="soutlet">
<option <?php if($POST['soutlet']==7) echo 'select="selected"'; ?> value="7">July</option>
</select>
Year
<select name="soutletto" id="soutletto">
<option <?php if($POST[soutletto]==2018) echo 'select="selected"'; ?> value="2018">2018</option>
</select>

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>';
}

In php: How to set <select name=[php variable name] >?

for($i=0; $i++; $i<4)
{
$nameselected = "a".$i;
echo "<select name="$nameselected" onChange='document.changet_form.submit()'>
<option value="apple">Volvo</option>
<option value="orange">Saab</option>
<option value="grape">Opel</option>
<option value="mango">Audi</option>
</select>";
}
Get the name of :
if(($_REQUEST[$nameselected]) == "a0")
Hi, How to set the select name=[php variable name] ?
The way i set and get is it correct?
THANK YOU :)
This should work:
for($i = 0; $i < 4; $i++) {
$nameselected = "a".$i;
echo '<select name="' . $nameselected . '" onChange="document.changet_form.submit()">
<option value="apple">Volvo</option>
<option value="orange">Saab</option>
<option value="grape">Opel</option>
<option value="mango">Audi</option>
</select>';
}
I think you made a typo in your for loop!

current years in dropdown select box

I have this function for print current year in select box but option value not equal with option text. i.e :current years in selected value is 2013 but html text output is 2012. how to fix this?
PHP:
<select name="year">
<?php
for($i=date("Y")-5;$i<=date("Y");$i++) {
$sel = ($i == date('Y')) ? 'selected' : '';
echo "<option value=".$i." ".$sel.">".date("Y", mktime(0,0,0,0,1,$i))."</option>";
}
?>
</select>
Output:
<select name="year">
<option value=2008 >2007</option>
<option value=2009 >2008</option>
<option value=2010 >2009</option>
<option value=2011 >2010</option>
<option value=2012 >2011</option>
<option value=2013 selected>2012</option>
</select>
Maybe not a good wayو But just Add +1 for $i:
<select name="year">
<?php
for($i=date("Y")-5;$i<=date("Y");$i++) {
$sel = ($i == date('Y')) ? 'selected' : '';
echo "<option value=".$i." ".$sel.">".date("Y", mktime(0,0,0,0,1,$i+1))."</option>"; // change This Line
}
?>
</select>
Online Demo Here
Problem with your value printing
<select name="year">
<?php
for($i=date("Y")-5;$i<=date("Y");$i++) {
$sel = ($i == date('Y')) ? 'selected' : '';
echo "<option value=".$i." ".$sel.">".$i."</option>"; // here I have changed
}
?>
</select>
<?php
$yearArray = range(2015, 2050);
?>
Year
<select name="year" id="year">
<option value="">Select Year</option>
<?php
foreach ($yearArray as $year) {
$selected = ($year == 2017) ? 'selected' : '';
echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>
</select>

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.

Categories