I want to display all month with current year but it must be start with current month like below.
I need like if current month October and year 2019 then option list should be start with 2019-10 then all renaming month with current year like below.
Output
<select>
<option value="">SELECT MONTH</option>
<option value="2019-10">October-2019</option>
<option value="2019-11">November-2019</option>
<option value="2019-12">December-2019</option>
<option value="2019-01">January-2019</option>
<option value="2019-02">February-2019</option>
<option value="2019-03">March-2019</option>
<option value="2019-04">April-2019</option>
<option value="2019-05">May-2019</option>
<option value="2019-06">June-2019</option>
<option value="2019-07">July-2019</option>
<option value="2019-08">August-2019</option>
<option value="2019-09">September-2019</option>
</select>
It's Working and give me exact output as i want.
<select>
<option value="">SELECT MONTH</option>
<?php
for ($i = 0; $i < 12; $i++)
{
$getMonth = strtotime(sprintf('%d months', $i));
$monthLabel = date('F', $getMonth)."-".date("Y");
$monthval = date("Y")."-".date('m', $getMonth); ?>
<option value="<?php echo $monthval; ?>"><?php echo $monthLabel; ?></option>
<?php }
?>
</select>
With this PHP code you can get current_month
date_default_timezone_get();
$month= date('Y-m', time());
Then you simply need to fill your 'select' tag with options in the correct order
Use the DateTime class. Create an array as follows:
$dt = date_create('first Day of this Month 00:00'); //start
$valueCaption = [];
$numberOptions = 5;
for($i=0;$i<$numberOptions;$i++){
$valueCaption[$dt->format('Y-m')] = $dt->format('F-Y');
$dt->modify('+1 Month');
}
returns a array $valueCaption how
array (
'2019-10' => "October-2019",
'2019-11' => "November-2019",
'2019-12' => "December-2019",
'2020-01' => "January-2020",
'2020-02' => "February-2020",
)
You use this array when outputting with a foreach ($valueCaption as $value => $caption) to create your options.
Related
I have a form that has five input fields:
name="MultiRoomFromDate" //outputs the date in the following format "d-m-Y"
name="MultiRoomFromTimeH" // has a dropdown of "00" to "23"
name="MultiRoomFromTimeM" // has a dropdown of "00" to "59"
<select name="MultiFromDisplayTimeH" id="MultiFromDisplayTimeH" class="FormTime">
<option value="0">0</option>
<option value="3600">1</option>
<option value="7220">2</option>
<option value="10800">3</option>
<option value="14400">4</option>
</select>
<select name="MultiFromDisplayTimeM" id="MultiFromDisplayTimeM" class="FormTime">
<option value="0">0</option>
<option value="900">15</option>
<option value="1800">30</option>
<option value="2700">45</option>
</select>
If I select the "MultiRoomFromDate" as 26-01-2020" and the MultiRoomFromTimeH as "09" and the MultiRoomFromTimeM as "00" and the select the "MultiFromDisplayTimeH" as "01"and the "MultiFromDisplayTimeM" as "00" and submit the form the output is:
FromDate = MultiRoomFromDate; //26-1-2020
FromHours = MultiRoomFromTimeH; //09
FromMins = MultiRoomFromTimeM; //00
DisplayHours = MultiFromDisplayTimeH; //3600
DisplayMins = MultiFromDisplayTimeM; //0
What I need to do is output the "FromDate, FromHours, FromMins" MINUS the "DisplayHours" and "DisplayMins" as Y-m-d H:i.
This is what I have been working with:
$MultiRoomFromDate = trim($_POST['MultiRoomFromDate']);
$MultiFromDisplayTimeH = trim($_POST['MultiFromDisplayTimeH']);
$MultiFromDisplayTimeM = trim($_POST['MultiFromDisplayTimeM']);
$FromdateStamp = strtotime($MultiRoomFromDate);
$TodateStamp = strtotime($MultiRoomToDate );
$DisplayTime = $FromTimeStamp - $FromDisplayHours - $FromDisplayMins;
$DisplayDateTime = date("Y-m-d H:i", $DisplayTime);
$RoomFromTime = $MultiRoomFromTimeH.":".$MultiRoomFromTimeM;
if(!empty($MultiFromDisplayTimeH)) {
$DisplayTime1 = date('Y-m-d H:i:s', strtotime("-". $MultiFromDisplayTimeH." hours", strtotime($MultiRoomFromDate." ". $RoomFromTime)));
}
if(!empty($MultiFromDisplayTimeM)) {
$DisplayTime = date('Y-m-d H:i:s', strtotime("-". $MultiFromDisplayTimeM." minutes", strtotime($DisplayTime1)));
}
if(empty($MultiFromDisplayTimeH) && empty($MultiFromDisplayTimeM)){
$DisplayTime = date('Y-m-d H:i', strtotime($DisplayDateTime));
}
Also tried:
$t = $MultiFromDisplayTimeH + $MultiFromDisplayTimeM;
$RoomFromTimeStamp = strtotime($MultiRoomFromDate);
$h = $RoomFromTimeStamp - $t;
$DisplayTime = date("Y-m-d H:i:s",$h);
First, simplify your select option syntax by removing the value attributes -- it only makes your markup harder to comprehend at a glance and it will not be necessary for the datetime calculation that I will later demonstrate. I also think simpler field names would be beneficial.
<select name="subtractHours" class="FormTime">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
<select name="subtractMinutes" class="FormTime">
<option>0</option>
<option>15</option>
<option>30</option>
<option>45</option>
</select>
Then, use PHP's datetime class to:
Initialize a datetime object from your date/time values
Subtract the submitted amount of time
Adjusted datetime string into the desired format.
Code: (Demo)
$fromDate = '26-1-2020';
$fromHours = '09';
$fromMins = '00';
$subtractHours = '1';
$subtractMins = '0';
$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat('d-m-Y H:i:s', "{$fromDate} {$fromHours}:{$fromMins}:00");
$date->sub(new DateInterval("PT{$subtractHours}H{$subtractMins}M"));
echo $date->format('Y-m-d H:i');
Output:
2020-01-26 08:00
I added the :00 as seconds to the object for stability/consistency in case you wanted to insert the full datetime stamp into your database (which will require Y-m-d H:i:s format).
The above will process just as well when the time subtraction results in losing a day. (Demo)
$fromDate = '25-1-2020';
$fromHours = '01';
$fromMins = '30';
$subtractHours = '6'; // don't bother with 3600 precalculation
$subtractMins = '45';
// Output: 2020-01-24 18:45
I completely rewrote my code and I have now resolved the issue by using the following code:
$DisplayTime = $FromTimeStamp - $FromDisplayHours - $FromDisplayMins;
echo "DISPLAYTIME 1 ".$DisplayTime."<br/>";
$DisplayDateTime = date("Y-m-d H:i", $DisplayTime);
Is there a shorter way to produce this html code with PHP?
Basically it's just a select input with hours of work options (every 30 minutes is a another option).
<select>
<option value="0" selected="selected" >From:</option>
<option value="8:00" >8:00</option>
<option value="8:30" >8:30</option>
<option value="9:00" >9:00</option>
<option value="9:30" >9:30</option>
<option value="10:00" >10:00</option>
<option value="10:30" >10:30</option>
<option value="11:00" >11:00</option>
<option value="11:30" >11:30</option>
<option value="12:00" >12:00</option>
<option value="12:30" >12:30</option>
<option value="13:00" >13:00</option>
<option value="13:30" >13:30</option>
... And so on up to 7:30 ...
<option value="7:30" >7:30</option>
</select>
This works. probably a little smaller.
while ($o <= 47) {
$date = new DateTime("08:00:00");
$date->add(new DateInterval("PT".($o*30)."M"));
echo '<option value="'. $date->format('H:i') .'" >'. $date->format('H:i') .'</option>';
$o ++;
}
The loop is fourty seven as there's 24*2 increments minus one hour.
will output the following:
<select>
<option value="08:00" >08:00</option><option value="08:30" >08:30</option><option value="09:00" >09:00</option><option value="09:30" >09:30</option><option value="10:00" >10:00</option><option value="10:30" >10:30</option><option value="11:00" >11:00</option><option value="11:30" >11:30</option><option value="12:00" >12:00</option><option value="12:30" >12:30</option><option value="13:00" >13:00</option><option value="13:30" >13:30</option><option value="14:00" >14:00</option><option value="14:30" >14:30</option><option value="15:00" >15:00</option><option value="15:30" >15:30</option><option value="16:00" >16:00</option><option value="16:30" >16:30</option><option value="17:00" >17:00</option><option value="17:30" >17:30</option><option value="18:00" >18:00</option><option value="18:30" >18:30</option><option value="19:00" >19:00</option><option value="19:30" >19:30</option><option value="20:00" >20:00</option><option value="20:30" >20:30</option><option value="21:00" >21:00</option><option value="21:30" >21:30</option><option value="22:00" >22:00</option><option value="22:30" >22:30</option><option value="23:00" >23:00</option><option value="23:30" >23:30</option><option value="00:00" >00:00</option><option value="00:30" >00:30</option><option value="01:00" >01:00</option><option value="01:30" >01:30</option><option value="02:00" >02:00</option><option value="02:30" >02:30</option><option value="03:00" >03:00</option><option value="03:30" >03:30</option><option value="04:00" >04:00</option><option value="04:30" >04:30</option><option value="05:00" >05:00</option><option value="05:30" >05:30</option><option value="06:00" >06:00</option><option value="06:30" >06:30</option><option value="07:00" >07:00</option><option value="07:30" >07:30</option></select>
For fun:
foreach(range(strtotime('8:00'), strtotime('19:30'), 1800) as $time) {
$val = date('g:i', $time);
echo '<option value="'.$val.'">'.$val.'</option>';
}
Create a range of times in 30 minute increments (1800 seconds)
Loop this range and convert the time into the correct format
Use this formatted time in your options
If you run this across a date/time in which daylight savings time is changed then it will not function correctly. You could alleviate this by setting to a timezone that does not have daylight savings time:
date_default_timezone_set('UTC');
For example:
<?php
$start = 8 * 60 * 60;
$step = 30 * 60;
$opts = '';
for ($i = 0; $i < 10; $i++) {
$time = date('H:i', $start + $i * $step);
$opt = <<<OPT
<option value="{$time}">{$time}</option>
OPT;
$opts .= $opt;
}
echo <<<OPTS
<select>
<option value="0" selected="selected" >From:</option>
{$opts}
</select>
OPTS;
You can use for loop to echo <option> tag and generating time like this.
<select>
<option value="0" selected="selected" >From:</option>
<?
$hour = 7;
$min = 30;
for ($x=0;$x < 48;$x++) {
if ($min < 30) {
$min += 30;
} else {
if ($hour < 24) {
$min = 0;
$hour += 1;
} else {
$hour = 1;
$min = 0;
}
}
$hour = sprintf("%02d", $hour);
$min = sprintf("%02d", $min);
echo '<option value="'.$hour.':'.$min.'">'.$hour.':'.$min.'</option>';
}
?>
</select>
Note 1: It is not tested and may not work properly.
Note 2: I also know, "it's not short!"
Update
I added leading zero before single digit numbers to match your format.
I am trying to create a HTML select input based on months/years and I am not sure the best approach for it.
While creating a standard loop of years was the first place I started, I wasn't sure how to get the last days of each of the month/years in order to create the values.
Is there any built in PHP functions that can accomplish something close to what I am looking for?
Logic:
Must be able to provide a starting year (ex. 2014).
Must start at the starting year and go 1 year past the current year (ex. If Start = 2014, it will show 2014, 2015, 2016.
The value of the option must be the last day of the month in date format (ex. <option value="01/31/2015">January 2015</option>
Output:
<select class="s2 span4" name="desiredCompletion" placeholder="Select a Desired Completion Date">
<optgroup label="2016">
<option value="01/31/2016">Janurary 2016</option>
<option value="02/28/2016">Feburary 2016</option>
<option value="03/31/2016">March 2016</option>
...
</optgroup>
<optgroup label="2015">
<option value="01/31/2015">Janurary 2015</option>
<option value="02/28/2015">Feburary 2015</option>
<option value="03/31/2015">March 2015</option>
...
</optgroup>
<optgroup label="2014">
<option value="01/31/2014">Janurary 2014</option>
<option value="02/28/2014">Feburary 2014</option>
<option value="03/31/2014">March 2014</option>
...
</optgroup>
</select>
This function should do:
function createYearSelect($from) {
// open select
echo "<select class=\"s2 span4\" name=\"desiredCompletion\" placeholder=\"Select a Desired Completion Date\">";
// for each year from the one specified to the one after current
foreach(range($from, date('Y') + 1) as $year) {
// open optgroup for year
echo "<optgroup label=\"$year\">";
// foreach month
foreach (range(1, 12) as $month) {
// timestamp of first day
$time = strtotime("$year-$month-01");
// create option with date() formatting
echo "<option value=\"".date("m/t/Y", $time)."\">".date("F Y", $time)."</option>";
}
// close optgroup for year
echo "</optgroup>";
}
// close select
echo "</select>";
}
Here's a bit more elegant way
foreach(range(2014, 2015) as $year){
foreach(range(1, 12) as $month){
echo date('Y.m.d', strtotime('Last day of ' . date('F', strtotime($year . '-' . $month . '-01')) . $year)) . PHP_EOL;
}
}
This outputs the list of the last days of months for the years in specified range.
2014.01.31
2014.02.28
2014.03.31
2014.04.30
...
Using the DateTime class you can create dates using natural language.
Eg,
$date = new DateTime("-1 Year");
echo $date->format('F Y'); // Prints 'December 2014'
You can use a decrementing for loop to create many dates going back in time.
Check for the function below which gives the total number of days in a month
date("t");
Further details are available at http://php.net/manual/en/function.date.php
I have this custom function in place, It will get the current year from the date function in php. When next year will come I want to append next year with previous year.
Example:
Current Year is "2013"
Next year will be "2014"
When year will come, It should dynamically add 2014 to drop-down list.
<select>
<option>2014</option>
<option>2013</option>
</select>
function ec_year(){
$year = date("Y");
}
Anyone any help please.
Use this :
$start_year = '2013';
$current_year = date("Y");
echo '<select>';
for ($i=$start_year; $i <= $current_year; $i++) {
echo '<option>' . $i .'</option>';
}
echo '</select>';
Try this give the end year as you like ...it will automatically work on present year
<select>
<?php for($i=date(Y);$i>=endyear);$i--){
<option><?php echo $i;?></option>
<?php }?></select>
Try this:
<select>
<option><?php print(Date("Y") + 1); ?></option>
<option><?php print(Date("Y")); ?></option>
</select>
Try:
echo date('Y')+1; #will give current year + 1
I was wondering if someone could help me.
I want to display a dropdown ( select ) box with 10 years starting with the current year.
I know displaying the year is done by using
date("Y");
But im not sure how to display the 10 years after that automatically.
Any help would be greatly appreciated.
Cheers
Try:
echo('<select name="year">');
for ( $i = date("Y"); $i < date("Y")+11; $i++ )
{
echo('<option value="'.$i.'">'.$i.'</option>');
}
echo('</select>');
See result here http://codepad.viper-7.com/8S0Ogi
PHP has a slew of Date/Time functions.
One you have instantiated a DateTime Object, you'll be able to manipulate any date or time using simple date_add or date_sub functions...
However, for your situation, it would be possible and a lot simpler to simply iterate over the number of years you want in the dropdown and simply increase the value for each option.
$yearSpan = 10;
$currentYear = date("Y");
$html = '<select id="foobar">';
for($i = $currentYear; $i<=$yearSpan; $i++) {
$html .= "<option value='".$i."'>".$i."</option>";
}
$html .= '</select>';
That should populate $html with content similar to this -
<select id="foobar">
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
...
<option value="2021">2021</option>
<option value="2022">2022</option>
</select>
You can try with this:
<select>
<option>Select One</option>
<?php
$year = date("Y");
$yearto = date("Y")+10;
while ($year <= $yearto)
{
echo "<option value='".$year."'>".$year."</option>";
$year++;
}
?>
</select>