Get date, explode, use data to select options in <select> - php

I have three variables set, $date_legible (in the format of F jS, Y, i.e. January 1st, 2011), $date_timestamp (self-explanatory), and $date_normal (in the format of YYYY-MM-DD)
I have three select fields:
<select name="date_mo">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="date_dy">
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">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>
<select name="date_yr">
<?php for($year=$curryear;$year<$curryear+51;$year++){ ?>
<option value="<?php echo $year; ?>"><?php echo $year; ?></option>
<?php } ?>
</select>
I know I want to convert the timestamp, but I will ultimately need to explode the output for the $month, $day, and $year. The problem is I don't know how to check the selects to the values and automatically select the correct option.
Can anyone help? Thanks!
UPDATE
I now have one more variable, because I'm having users submit from the front end, and Gravity Forms submits as YYYY-MM-DD.
Prior to diving into the solutions already posted (thank you guys [and gals] by the way), here's a little more information:
I'm running WordPress and have three custom fields as described above. When a user submits a post from the front end, the only custom field that's filled out is $date_normal. This is okay, because a post has to be moderated (i.e. published) from the backend. What I NOW need is on load to set the YYYY-MM-DD to set the options on the select. I'm not asking you guys to alter your answers, I'm sure I can figure it out (though if you can help with this new problem, it would be greatly appreciated).

If I understand your question correctly, you have a timestamp and you need to use the timestamp to select the appropriate select options when you render the page.
As a forewarning - this will make it possible to select invalid dates, for example February 31st (does not exist). You may want to consider re-drawing the year drop-down when the month is changed using javascript. Since you didn't specify any javascript library (and it is not clear how important the possibility of an invalid date is), I will not venture to show code for that, but it would not be terribly difficult.
// using the current time for the sake of the example, your timestamp would take the place of this
$timestamp = time();
// determine the selected month, day, and year
$selected_month = date('n', $timestamp);
$selected_day = date('j', $timestamp);
$selected_year = date('Y', $timestamp);
// now, create the drop-down for months
$month_html = '<select name="date_mo">';
for ($x = 1; $x < 13; $x++) {
$month_html .= '<option value='.$x.($selected_month == $x ? ' selected=true' : '' ).'>'.date("F", mktime(0, 0, 0, $x, 1, $selected_year)).'</option>';
}
$month_html .= '</select>';
// output
print $month_html;
// create the day drop-down
$day_html = '<select name="date_day">';
for ($x = 1; $x < 32; $x++) {
$day_html .= '<option value='.$x.($selected_day == $x ? ' selected=true' : '' ).'>'.$x.'</option>';
}
$day_html .= '</select>';
// output
print $day_html;
// create the year drop-down
$year_html = '<select name="date_year">';
$start_year = date('Y', time());
$max_year = $start_year + 51;
for ($x = $start_year; $x < $max_year; $x++) {
$year_html .= '<option value='.$x.($selected_year == $x ? ' selected=true' : '' ).'>'.$x.'</option>';
}
$year_html .= '</select>';
// output
print $year_html;
Output:
EDIT
If your source data will be in the format of YYYY-MM-DD. In order to translate that into a timestamp, and thus work with the example code I've posted, is to call strtotime on that formatted date value. Thus, the only line in the code above that would need to be changed is $timestamp = strtotime($the_formatted_date);

<?php
// $months = array(1 => 'January', 2 => 'February', ... etc
?>
<select name="date_mo">
<? for ($i = 1, $current = (int) date('n'); $i <= 12; $i++) { ?>
<option value="<?=$i;?>"<?=($current === $i ? ' selected="selected"' : NULL);?>><?=$months[$i];?></option>
<? } ?>
</select>
<select name="date_dy">
<? for ($i = 1, $current = (int) date('j'); $i <= 31; $i++) { ?>
<option value="<?=$i;?>"<?=($current === $i ? ' selected="selected"' : NULL);?>><?=$i;?></option>
<? } ?>
<select name="date_yr">
<? for ($i = $current = (int) date('Y'), $until = $i + 50; $i <= $until; $i++) { ?>
<option value="<?=$i;?>"<?=($current === $i ? ' selected="selected"' : NULL);?>><?=$i;?></option>
<? } ?>
</select>

I would personally convert the 3 fields to a more user-friendly date selector (using for example jQuery UI), but to have the correct option selected when you open the page, you need to add a check to each option.
Example for the year:
<?php
$selected_year = date("Y", $date_timestamp); // get the selected year
for($year=$curryear;$year<$curryear+51;$year++)
{
?>
<option value="<?php echo $year; ?>" <?php echo ($selected_year == $year) ? 'selected' : ''; ?>><?php echo $year; ?></option>
<?php
}
?>

Related

How can I achieve years on drop down menu

Hello everyone one I want to get this kind of drop down menu which has years need to be selected
For example season 2021/2022, 2020/2021, all these value should be generated from 1990 to 2022
If I understood correctly, you want to populate the select with options with all seasons until now since a specific year?
You could modify a for loop so the years appears in a descending order, and then output the option tag with the "previous year / year" as a value like this:
<div class="season-select-wrapper">
<?php
// Define variables for the select
$currentYear = 2022;
$startYear = 1990;
?>
<label for="season">Select a season</label>
<select name="season" id="season">
<?php for ($year = $currentYear; $year > $startYear; $year--) { $prevYear = $year - 1; ?>
<option value="<?php echo "{$prevYear}/{$year}"; ?>"><?php echo "{$prevYear}/{$year}"; ?></option>
<?php } ?>
</select>
</div>
This would create HTML markup like this:
<div class="season-select-wrapper">
<label for="season">Select a season</label>
<select name="season" id="season">
<option value="2021/2022">2021/2022</option>
<option value="2020/2021">2020/2021</option>
<option value="2019/2020">2019/2020</option>
<option value="2018/2019">2018/2019</option>
<option value="2017/2018">2017/2018</option>
<option value="2016/2017">2016/2017</option>
<option value="2015/2016">2015/2016</option>
<option value="2014/2015">2014/2015</option>
<option value="2013/2014">2013/2014</option>
<option value="2012/2013">2012/2013</option>
<option value="2011/2012">2011/2012</option>
<option value="2010/2011">2010/2011</option>
<option value="2009/2010">2009/2010</option>
<option value="2008/2009">2008/2009</option>
<option value="2007/2008">2007/2008</option>
<option value="2006/2007">2006/2007</option>
<option value="2005/2006">2005/2006</option>
<option value="2004/2005">2004/2005</option>
<option value="2003/2004">2003/2004</option>
<option value="2002/2003">2002/2003</option>
<option value="2001/2002">2001/2002</option>
<option value="2000/2001">2000/2001</option>
<option value="1999/2000">1999/2000</option>
<option value="1998/1999">1998/1999</option>
<option value="1997/1998">1997/1998</option>
<option value="1996/1997">1996/1997</option>
<option value="1995/1996">1995/1996</option>
<option value="1994/1995">1994/1995</option>
<option value="1993/1994">1993/1994</option>
<option value="1992/1993">1992/1993</option>
<option value="1991/1992">1991/1992</option>
<option value="1990/1991">1990/1991</option>
</select>
</div>

Create dynamic <select> with PHP

I have a $_SESSION variable $_SESSION['invoiceDate'] that is populated with a value representing YYYYMM.
I need to dynamically build a select element so that I have 6 options going back from the 'invoiceDate' e.g. if $_SESSION['invoiceDate'] = 201602 the output should be...
<select id="invoiceDate" name="invoiceDate">
<option value="201602">Feb 2016</option>
<option value="201601">Jan 2016</option>
<option value="201512">Dec 2015</option>
<option value="201511">Nov 2015</option>
<option value="201510">Oct 2015</option>
<option value="201509">Sep 2015</option>
</select>
My PHP is very basic... I can split the session value and assign the first 4 digits to $year and the other 2 to $month. Then loop 6 times... Each time I echo to output the <option> line concatenating the year and month to make the value and then subtracting 1 from the month and if month=0 then take 1 from year and make month 12. This gives me....
<option value="201602"></option>
<option value="201601"></option>
<option value="201512"></option>
<option value="201511"></option>
<option value="201510"></option>
<option value="201509"></option>
Now I can join the year and month with a hyphen to give
<option value="201602">2016-02</option>
<option value="201601">2016-01</option>
<option value="201512">2015-12</option>
<option value="201511">2015-11</option>
<option value="201510">2015-10</option>
<option value="201509">2015-09</option>
But how do I get a Month Year instead... e.g. Feb 2016 instead of 2016-02?
Why aren't you using DateTime?
$e = '201602';
$date = new DateTime($e);
echo $date->format('M');
outputs Feb
In your case it could be
$e = '201602';
$date = new DateTime($e);
$month = $date->format('n');
for($i=0;$i<6;$i++)
{
echo '<option value="'.$date->format('Ym').'">'.$date->format('M Y').'</option>';
$date = $date->sub(new DateInterval('P1M'));
}
This outputs
<option value="201602">Feb 2016</option>
<option value="201601">Jan 2016</option>
<option value="201512">Dec 2015</option>
<option value="201511">Nov 2015</option>
<option value="201510">Oct 2015</option>
<option value="201509">Sep 2015</option>
Just edit this as you need and don't use that infernal version of date with strtotime...
$date = "201602";
for($i=1;$i<=6;$i++){
echo date('M, Y',strtotime(date('Y-m-d',strtotime($date))." -$i month"));
}
In this loop we are deleting 1 month from your date.
Date Parameters
This is it:
<?php $session_val = "201602";?>
<select id="invoiceDate" name="invoiceDate">
<?php
for($i = 0; $i < 6; $i++){
//$text = date('Y-m', strtotime(date('Y-m-d', strtotime($session_val))." -$i month")); // for 2016-02
$text = date('M Y', strtotime(date('Y-m-d', strtotime($session_val))." -$i month")); //for Feb 2016
$val = date('Ym', strtotime(date('Y-m-d', strtotime($session_val))." -$i month"));
echo '<option value="'.$val.'">'.$text.'</option>';
}?>
</select>
I did this in my local, but when i am going to post i see some one
post similar thing. But I did so i post.

HTML <select> default option

I know this question has been asked before, but I have an unaddressed concern about it.
How can you have a default value that is generated by serverside script?
Ex:
Weekday: <select name="weekday" class="right" value="<% =rs("Weekday")%>">
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
I know you can have the selected attribute with regular elements, but I'm thrown for a loop here.
Any ideas?
You want your HTML to look something like this eventually - notice the selected after "Friday" to select Friday by default:
<select name="weekday" class="right">
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday" selected>Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
A quick and dirty PHP example can be used like so, where each option is asked to verify whether it is the correct option for the current date. If it is, show selected otherwise don't show anything. You can try this code on http://phpfiddle.org/lite.
<html>
<body>
<select name="weekday" class="right">
<option value="Monday" <?php echo setSelected('Monday'); ?>>Monday</option>
<option value="Tuesday" <?php echo setSelected('Tuesday'); ?>>Tuesday</option>
<option value="Wednesday" <?php echo setSelected('Wednesday'); ?>>Wednesday</option>
<option value="Thursday" <?php echo setSelected('Thursday'); ?>>Thursday</option>
<option value="Friday" <?php echo setSelected('Friday'); ?>>Friday</option>
<option value="Saturday" <?php echo setSelected('Saturday'); ?>>Saturday</option>
<option value="Sunday" <?php echo setSelected('Sunday'); ?>>Sunday</option>
</select>
</body>
</html>
<?php
function setSelected($day)
{
date_default_timezone_set('America/Chicago');
return strtolower($day) === strtolower(date('l')) ? ' selected' : '';
}
?>
I'd be more inclined to create the select/option on the server side itself. Again, quick and dirty PHP example:
<html>
<body>
<select name="weekday" class="right">
<?php
date_default_timezone_set('America/Chicago');
$dayOfWeek = strtolower(date('l'));
$days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
foreach ($days as $day)
{
echo sprintf("<option value=\"%s\" %s>%s</option>\n",
$day,
$dayOfWeek == strtolower($day) ? 'selected' : '',
$day
);
}
?>
</select>
</body>
</html>

From and To date search in database PHP

Good day! I am trying to create a "search by date" option in my website that will retrieve the records in the database whose dates matched the users' input. I have these select boxes:
<!-- FROM -->
<select name="from_month" id="from_month">
<option selected>-- MONTH --</option>
<option value="january">January</option>
...
<option value="december">December</option>
</select>
<select name="from_day" id="from_day">
<option selected>-- DAY --</option>
<?php $i = 1;
while ($i <= 31) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<select name="from_year" id="from_year">
<option selected>-- YEAR --</option>
<?php $i = date('Y');
while ($i <= 2040) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<!-- FROM (END) -->
<!-- TO -->
<select name="to_month" id="to_month">
<option selected>-- MONTH --</option>
<option value="1">January</option>
...
<option value="12">December</option>
</select>
<select name="to_day" id="to_day">
<option selected>-- DAY --</option>
<?php $i = 1;
while ($i <= 31) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<select name="to_year" id="to_year">
<option selected>-- YEAR --</option>
<?php $i = date('Y');
while ($i <= 2040) {
echo '<option value="'.$i.'">'.$i.'</option>';
$i++;
}
?>
</select>
<!-- TO (END) -->
What I originally plan to do is get all the "from_" values and store it into a variable '$from'. The same goes for the "to_". Because my database for dates looks like this:
id | title(text) | date_submitted(date) |
1 | Lorem Ipsum | 2015-01-20 |
Any help would be appreciated on how I would accomplish this. Thank you in advance!
Since you know every part of the date, MySQL's BETWEEN would do the trick:
SELECT * FROM db WHERE mydate BETWEEN 'from_date' AND 'to_date'
from_date and to_date are your variables, get through the form.
As #AnoopS stated, use numbers to value your month boxes.
And remember to prevent injections using mysqli or PDO with prepared statements.

php mysql not updating row in proper way

I have a column which name is possession1, in this column I concat the $month.$year, but problem is when I update only $month then $year value is laps but when I update only $year then $month do not laps but $year update the column with old value and new value like,If month is May and old year is 2013, and when I update only year with 2014 then its showing output May20132014, this is my code
<select id="month" name="month" >
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
</select>
<select id="year" name="year">
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
$month=#$_POST['month'];
$year=#$_POST['year'];
$arr = array($month, $year);
$possession1 = join("", $arr);
$updateSale="update sale set possession='$possession1' where id='$update_id'";
$r=mysql_query($updateSale);
Try this, Use isset to check the post value,
if(isset($_POST['month']) && isset($_POST['year'])){
$month = $_POST['month'];
$year = $_POST['year'];
$possession1 = $month. " ".$year;
$updateSale="update sale set possession='".$possession1."' where id='$update_id'";
$r=mysql_query($updateSale);
}
Correct your query to this--
$updateSale="update sale set possession='".$possession1."' where id='".$update_id."'";

Categories