Getting previous months in php - php

I need to display 3 month from the previous month in php
Here is what I have tried so far
<select style='width: 112px;' name='PayMonth'>
<option name="PayMonth" value=''>Select Month</option>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php echo $i; ?>" ><?php echo date("F",mktime(0,0,0,$i,1,date("Y")));?>
</option>
<?php
}?>
This will display all months
But how can I display on 3 months from current month
i.e.,
If current month is april it should show Feb, Mar, Apr
If current month is jan it should show nov, dec, jan
I tried..
<?php
$month = date("m");
?>
But in the loop I am confused with
for($i = 1 ; $i <= $month; $i++)

If you are getting $month = date("m"); Then your forloop should be
for($i = $month-3 ; $i <= $month; $i++)
Sidenote :
As you are using
<option value="<?php echo $i; ?>" ><?php echo date("F",mktime(0,0,0,$i,1,date("Y")));?>
You may get -1, -2 inside the values. So you should change
<option value="<?php echo date("m",mktime(0,0,0,$i,1,date("Y"))); ?>" ><?php echo date("F",mktime(0,0,0,$i,1,date("Y")));?>
To get
<select style='width: 112px;' name='PayMonth'>
<option name="PayMonth" value=''>Select Month</option>
<option value="11" >November </option>
<option value="12" >December </option>
<option value="01" >January </option>
<option value="02" >February </option>
</select>

Easy as cake:
echo date('Y-m-d', strtotime("first day of -1 month"));
And you can substitute -1 with the number of your choice:
<?php
foreach(range(-1,-3,-1) as $value) {
echo date('Y-m-d', strtotime(sprintf("first day of %d month", $value)));
}

Just do:
date('m', strtotime('-1 month'));
to get last month.
You can put -2 or -3 to get 2 months ago or 3 months ago also.
Substitute 'm' with the format of your choice.

Try this..
echo date('M', strtotime('0 month'));
echo date('M', strtotime('-1 month'));
echo date('M', strtotime('-2 month'));
echo date('M', strtotime('-3 month'));

Take into consideration that you will get wrong results when using months back using the current day - on every 31st day the month.
Try this instead:
<?
$monthsBack = 3;
// we need to do set day to middle of the month,
// otherwise "-x month" will return wrong results on the 31st of each month:
$now = mktime(0, 0, 0, date("m"), 15, date("Y"));
for ($i1=0; $i1<$monthsBack; $i1++) {
$displayDate = strtotime("-{$monthsBack} month", $now);
echo '<option value="'.date("m", $displayDate).'" >'.date("F", $displayDate).'</option>';
}
?>

// Today is 2015-02-30
echo date('Y-m-d', strtotime('last month'));

Related

Create Month And Year Dropdown Using PHP

I am looking for dynamically generate Month And Year Dropdown Menu using PHP from Month December 2021 to December 2025.
I need it like this
<select>
<option selected="" value="12-2021">December,2021</option>
<option value="1-2022">January,2022</option>
<option value="2-2022">February,2022</option>
<option value="3-2022">March,2022</option>
</select>
I have tried some codes like this
<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>
Its work for months but not able to combine it with years and values as per my requirements. I am new in PHP and not getting much idea how I can achieve it, Let me know if anyone here can help me for same. Thanks!
<?php
$start = new DateTime('2021-12-01');
$end = new DateTime('2025-12-01');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
?>
<select name="month" size='1'>
<?php foreach ($period as $dt)
echo "<option value= " . $dt->format("Y-m") . ">" . strftime('%B-%Y', $dt->format('U')) . "</option>";
?>
</select>

Populate drop down form with months and year php

I am trying to create two drop downs with HTML and PHP. The first is an auto submit that sends a month to a session variable:
<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<option value="">-- Select Month --</option>
<?php $month = date('n'); // current month
for ($x = 0; $x < 12; $x++) { ?>
<option value="<?php echo date('m', mktime(0,0,0,$month + $x,1)); ?>"
<?php echo date('m', mktime(0,0,0,$month + $x,1)) == $_SESSION['month'] ? "selected='selected'":""; ?> >
<?php echo date('F Y', mktime(0,0,0,$month + $x,1)); ?>
<?php } ?>
</select>
</form>
This works great. But I then need a second drop down with the day name and number:
<form action="" method="post">
<select name="day" onchange="this.form.submit()">
<option value="">-- Select Day --</option>
<?php for ($i = $current_day; $i < 31; $i++) {
$tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i;
$weekday = date('D', strtotime($tmp_date)); { ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
<?php echo $weekday." ".$i; ?>
<?php } ?>
<?php } ?>
</option>
<?php } ?>
</select>
</form>
This uses a temp date with a session variable as '2014' which I set before. I need to remove the session year variable and get the first drop down to know which year the month selected is and then pass this to the temp date so that it populates the day name and number correctly for the next year (2015) if you choose January onwards. At the moment it only shows the day name and number from the current year (2014) in this case.
<?php
if(isset($_POST)) {
$date = explode('-', $_POST['month']);
$year = $date[0];
$month = $date[1];
echo "Year: ".$year." Month: ".$month;
}
?>
<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<?php
for ($i = 0; $i <= 12; ++$i) {
$time = strtotime(sprintf('+%d months', $i));
$value = date('Y-m', $time);
$label = date('F Y', $time);
printf('<option value="%s">%s</option>', $value, $label);
}
?>
</select>
This gives me the year and month as separate values.
I had to check the print out of the first script, because it didn't make much sense...
<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<option value="">-- Select Month --</option>
<option value="11"
>
November 2014 <option value="12"
>
December 2014 <option value="01"
>
January 2015 <option value="02"
>
February 2015 <option value="03"
>
March 2015 <option value="04"
>
April 2015 <option value="05"
>
May 2015 <option value="06"
>
June 2015 <option value="07"
>
July 2015 <option value="08"
>
August 2015 <option value="09"
>
September 2015 <option value="10"
>
October 2015 </select>
</form>
Might be I'm missing something, because it seemed to work, but it doesn't seem like you're closing the option tags. You are doing this:
<option value="11">November 2014
<option value="12">December 2014
Instead of this:
<option value="11">November 2014</option>
<option value="12">November 2014</option>
This doesn't seem to be the issue though, at least not in chrome, because when I checked the head data being sent, it was just fine:
month: 11
What happens when you send in the variable is that whatever page you send this form data with the method POST against will have to handle it further on. In PHP you do that with the global variable $_POST, in this case $_POST['month'].
if(!empty($_POST['month']))
{
echo $_POST['month'];
}
If you want to make it a session variable, the page you send the form data to will have to communicate with your browser to set this (through the reply HTTP header field). To do anything related to session in PHP you must first start a session using the session_start() before any HTML/data is sent against the browser, and the same goes for if you want to set a session variable using for example session_set_cookie_params. You also got setcookie, which also must be used before any data/HTML is sent to the browser and the variables can be accessed through $_COOKIE.
See if you can make some sense out of this, I've just used $_POST in this case, you can swap it with session or cookie and remember to send it before any data/html to the browser.
<?php
$selmonth = 0;
if(!empty($_POST['month']))
{
$selmonth = $_POST['month'];
}
$month = date('n'); // current month
echo '<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<option value="">-- Select Month --</option>';
for($i=0; $i<12; $i++)
{
$monthtime = mktime(0,0,0,$month + $i,1);
$monthnum = date('m', $monthtime);
echo '
<option value="'.$monthnum.'"'.
($monthnum == $selmonth ? ' selected="selected"' : '').
'>'.date('F Y', $monthtime).'</option>';
}
echo '
</select>
</form>';
?>
<select required="" class="btn btn-warning btn-raised" onchange="loadMonthYear(this)">
<?php
$fdt = date('Y-m-1');
$tdt = date('Y-m-1', strtotime("-12 months"));
while (date('Ymd', strtotime($fdt)) >= date('Ymd', strtotime($tdt))) {
echo '<option value="'.date('M, Y', strtotime($fdt)).'"><a data-tag="' . date('M, Y', strtotime($fdt)) . '" href="javascript: void(0);">' . date('M, Y', strtotime($fdt)) . '</option>';
$fdt = date('Y-m-1', strtotime("-1 months", strtotime($fdt)));
}
?>
</select>

get end date automatically using specific days in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
When Start date is given it should get the end date by fetching only sunday or mon, tue, wed or ,mon to fri or tue, thurs, sat using php. pls anyone help me.....
Here is my html code:
<label>Start Date:</label><input type="text" id="txtStartDate" name="sd"></input>
<td>days</td>
<td>
<select name="days">
<option value="" >only sunday</option>
<option value="batch">sat and sun</option>
<option value="">M-F</option>
<option value="">M-W-F</option>
<option value="">TU-THUR-SAT</option>
</td>
<td>
<input type="submit" value="Submit" name="sub" />
</td>
<?php
//$startdate=$_POST['sd'];
$startdate='2013-12-25';
$date=date_create($startdate);
date_add($date,date_interval_create_from_date_string("10 days"));
echo date_format($date,"Y-m-d");
?>
it only fetches using number of days but i want to get using specific days.
Store that days that you want to use in the option values ie
<select name="days">
<option value="sunday" >only sunday</option>
<option value="saturday-sunday">sat and sun</option>
<option value="monday-tuesday-wednesday-thursday-friday">Monday to Friday</option>
<option value="monday-wednesday">Only Monday or Wednesday</option>
</select>
if (isset($_POST['sd']) && isset($_POST['days'])) {
$startDate = new DateTime($_POST['sd']);
$days = explode('-', $_POST['days']);
$count = count($days);
$dates = array();
$currentDate = $startDate->format('Y-m-d');
$j = 0;
for ($i=0;$i<10;$i++) {
$day = $days[$j];
$a = new DateTime($currentDate);
$a->modify("next $day");
$dates[] = $currentDate = $a->format('Y-m-d');
$j = (($j+1) == $count) ? 0 : $j++;
}
foreach ($dates as $date) {
echo $date.'<br />';
}
}
Hope this helps! :)
EDIT
or if you only want the last one of the array:
instead of the foreach use
echo end($dates);
Consider using strtotime
<option value="SUNDAY" >only sunday</option>
<option value="SATSUN">sat and sun</option>
<option value="MF">M-F</option>
<option value="MWF">M-W-F</option>
<option value="TUTHURSAT">TU-THUR-SAT</option>
<?php
$option = //get selected option;
$startDate = //get selected date;
$days = array();
switch($option){
case 'SUNDAY' : $days[] = 'next sunday'; break;
case 'SATSUN' : $days[] = 'next saturday'; $days[] = 'next sunday'; break;
//etc
}
$count = 0;
$endDate = $startDate;
while ($count < 10){
$index = $count++ % count($days);
$endDate = date('Y-m-d' , strtotime($days[$index] , strtotime($endDate)));
}
echo $endDate;
Demo

Open an HTML select tag after one option has been selected

I have an HTML select tag like this:
<select name="since_date">
<option value="<?php $sixmonths = date('Y-m-d', strtotime('-6 months')); echo $sixmonths; ?>">6 months</option>
<option value="<?php $fivemonths = date('Y-m-d', strtotime('-5 months')); echo $fivemonths; ?>">5 months</option>
<option value="<?php $fourmonths = date('Y-m-d', strtotime('-4 months')); echo $fourmonths; ?>">4 months</option>
<option value="<?php $threemonths = date('Y-m-d', strtotime('-3 months')); echo $threemonths; ?>">3 months</option>
<option value="<?php $twomonths = date('Y-m-d', strtotime('-2 months')); echo $twomonths; ?>">2 months</option>
<option value="<?php $onemonth = date('Y-m-d', strtotime('-1 month')); echo $onemonth; ?>">1 month</option>
</select>
And I want when for example 4 months is selected a new select tag to appear like this one:
<select name="until_date_4months">
<option value="<?php $threemonths = date('Y-m-d', strtotime('-3 months')); echo $threemonths; ?>">3 months</option>
<option value="<?php $twomonths = date('Y-m-d', strtotime('-2 months')); echo $twomonths; ?>">2 months</option>
<option value="<?php $onemonth = date('Y-m-d', strtotime('-1 month')); echo $onemonth; ?>">1 month</option>
<option value="<?php $now = date('Y-m-d'); echo $now; ?>">Now</option>
</select>
Every time a period is chosen (Ex. 6 months, 5 months) a new select must appear with options lower than the selected one plus a new option "Now" like above. How is this possible? What is the approach? I want to use these information with a submit button. (Send with JQuery a GET request to a specific PHP file <- this I know how to handle).
I can for example use JavaScript like this:
function sinceDateValue(selection) {
if (selection.value == "<?php $fourmonths = date('Y-m-d', strtotime('-4 months')); echo $fourmonths; ?>") {
document.getElementbyId(until_date_4months).style.display = "block"
}
}
and use for the second submit id="until_date_4months" and style="display: none;". Also for the first submit onchange="testValue(this); but this will just show the submit and I want to send only two information with the form: since_date and until_date...
You can create the options dynamically using a loop:
<?php for ($i = 6; $i >= 0; $i--) { ?>
<option value="<?php echo date('Y-m-d', strtotime('-' . $i . ' months')); ?>">
<?php echo ($i == 0) ? 'Now' : $i . ' months'; ?>
</option>
<?php } ?>
As for the jQuery, you can grab the elements based on the one selected, and populate your select box with those:
$('select[name="since_date"]').on('change', function() {
var newOpts = $('option:selected', this).nextAll().clone();
$(newOpts).show().appendTo('#example');
});
Here's a complete example fiddle
You can do smomething like this, all you need to do is set the right values of the options.
$("#one").change(function(){
var htmlOf2 = "", d;
for(var i=1;i<parseInt($(this).find("option:selected").text()) + 1;i++)
{
d = new Date();
d = d.setMonth(d.getMonth() + (i * -1));
// This sets the right month, but is not set as a value yet. I don't know what format you want
htmlOf2 += "<option value=''>" + (i + " months") + "</option>";
}
htmlOf2 += "<option>NOW</option>";
$("#two").html(htmlOf2);
}).trigger("change");
Live fiddle: http://jsfiddle.net/5qaw7/1/

build select list based on current date php

Ive created a drop select list that generates a list of dates based on the current date,
4 days back and 7 days forward, Ive tried several shorter scripts but they all fail, ive been using the following but it has some limitiations... Thanks!
<option value=""> select </option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-4),date("Y")));?>" style="color:red;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-4),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-3),date("Y")));?>" style="color:red;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-3),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-2),date("Y")));?>" style="color:red;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-2),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-1),date("Y")));?>" style="color:red;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-1),date("Y")));?></option>
<option value="<?php echo date ('m/d/Y');?>"><?php echo date ('m/d/Y');?> TODAY </option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+1),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+1),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+2),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+2),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+3),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+3),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+4),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+4),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+5),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+5),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+6),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+6),date("Y")));?></option>
<option value="<?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+7),date("Y")));?>" style="color:green;font-weight:bold;"><?php echo date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")+7),date("Y")));?></option>
This should do what you're asking. I've rewritten your code to make it as simple and readable as possible.
<?php
echo '<select>';
echo '<option value="">select</option>';
for($i = -4; $i <= 7; ++$i)
{
$date = date('m/d/Y', strtotime($i.' days'));
echo '<option value="'.$date.'" style="color:red;">'.$date.'</option>';
}
echo '</select>';
?>
What you should be doing is to create a function for your calculation,
so that you can use mydate(+1) , mydate(-2) in your code instead of that spaggheti.
Don't mix (and repeat) so much php code in your html, it is very bad practice and hard to maintain.
Second, inside your function you could turn your time to unix timestamp with appropriate function, add the number of miliseconds and turn it back to whatever time format you need.
Did you try
<?php
for($i = -4; $i <= 7; $i++) {
$date = date ("m/d/Y", mktime (0,0,0,date("m"),(date("d")-$i),date("Y")));
$date_txt = $date;
if($i == 0) {
$date_txt = "Today";
}
printf("<option value=\"%s\" style=\"color: red\">%s</option>", $date, $date_txt);
}
?>
This should suffice:
echo '<select><option> select </option>';
for($x=-4;$x<=7;$x++){
$date = date("m/d/Y",(time() + (86400 * $x)));
if($x<0){
$color='red';
}elseif($x==0){
$color = 'black;font-weight:bold';
}else{
$color = 'green';
}
echo '<option style="color:'.$color.'" value="'.$date.'">'.($x==0 ? 'TODAY':$date).'</option>';
}
echo '</select>';

Categories