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
Related
I am developing a simple day calculator WordPress plugin for property sales. The point of the application is to give the user the 45th & 180th day after they close on their property.
The calculation converts Datetime to Epoch time and then adds 45/180 days worth of ticks and then converts the sums back into datetime. I figured this would be the best way to work around things such as leap years.
I got the PHP and basic HTML form working locally with no issues. However when I added the code to work inside a WordPress plugin I found that the calculator only works when the 21st of each month is selected. If any other date is selected I get a 404 error. Not exactly sure why this could be happening.
Here is a link to the plugin code https://github.com/Anth0nyBarb0sa/Days-Calculator-Plugin the code below is what I had running on localhost without any issues.
If you want to check out the plugin in action I created a test page on my website https://anthonybarbosadevelopment.com/test-page
<?php
//Return Name of Month
function getMonth($date)
{
switch ($date) {
case 1:
$month = 'January';
break;
case 2:
$month = 'Febuary';
break;
case 3:
$month = 'March';
break;
case 4:
$month = 'April';
break;
case 5:
$month = 'May';
break;
case 6:
$month = 'June';
break;
case 7:
$month = 'July';
break;
case 8:
$month = 'August';
break;
case 9:
$month = 'September';
break;
case 10:
$month = 'October';
break;
case 11:
$month = 'November';
break;
case 12:
$month = 'December';
break;
default:
$month = 'Not a valid month!';
break;
}
return $month;
}
//Initialize years 1 year behind and 4 years in advance
$yearCurrent = date('Y', time());
$yearBefore = $yearCurrent - 1;
$year1 = $yearCurrent + 1;
$year2 = $yearCurrent + 2;
$year3 = $yearCurrent + 3;
$year4 = $yearCurrent + 4;
//Initalize results
$dateTimeMSG = "";
$fourtyfiveMSG = "";
$oneeightyMSG = "";
$dateTime = "";
$fourtyfive = "";
$oneeighty = "";
if (isset($_GET['run'])) {
//initalize selected date
$month = $_GET['month'];
$day = $_GET['day'];
$year = $_GET['year'];
//Other
$seconds = 86400;
try {
//Convert to unix timestamp
$dateTime = new DateTime("{$year}-{$month}-{$day}");
$dateTime = $dateTime->format('U');
//Add 45 days
$fourtyfive = $dateTime + ($seconds * 45);
//Add 180 days
$oneeighty = $dateTime + ($seconds * 180);
//Convert to date time
function getWeekDay($date)
{
$weekday = date('N', $date);
switch ($weekday) {
case 1:
$weekday = "Monday";
break;
case 2:
$weekday = "Tuesday";
break;
case 3:
$weekday = "Wednesday";
break;
case 4:
$weekday = "Thursday";
break;
case 5:
$weekday = "Friday";
break;
case 6:
$weekday = "Saturday";
break;
case 7:
$weekday = "Sunday";
break;
default:
$weekday = "Not a valid Day!";
}
return $weekday;
}
function getDateTime($dateTime)
{
$weekday = getWeekDay($dateTime); // 1-7
$month = getMonth(gmdate("m", $dateTime));
$day = gmdate("d", $dateTime);
$year = gmdate("Y", $dateTime);
$dateTime = "{$weekday}, {$month} {$day}, {$year}";
return $dateTime;
}
$dateTime = getDateTime(($dateTime));
$fourtyfive = getDateTime(($fourtyfive));
$oneeighty = getDateTime(($oneeighty));
//Format to Message
$dateTimeMSG = "For <strong>{$dateTime}</strong>";
$fourtyfiveMSG = "Your 45-day Identification Period ends on midnight of: <strong>{$fourtyfive}</strong>";
$oneeightyMSG = "Your 180-day Identification Period ends on midnight of: <strong>{$oneeighty}</strong>";
$disclaimer1 = "<strong>Always verify your exchange deadlines with your tax advisor.</strong>";
$disclaimer2 = "<strong>Notice: </strong>The actual deadline for completing an exchange is the earlier of either 180 days from the date on which the Exchanger transfers the relinquished property, or the due date, including extensions filed by the Exchanger, for the Exchanger's tax return for the year of the transfer of the relinquished property. Consult your tax advisor regarding your tax filing requirement dates.";
} catch (Exception $e) {
$dateTimeMSG = "";
$fourtyfiveMSG = "";
$oneeightyMSG = "";
$dateTime = "";
$fourtyfive = "";
$oneeighty = "";
$disclaimer1 = "";
$disclaimer2 = "";
}
}
?>
<html>
<body>
<div id="calculator">
<div>
<p>Date the relinquished property (sale) was closed:</p>
<form class="days-form" action="<? $_SERVER['PHP_SELF'] ?>" method="get">
<div>
<label for="month">Month:</label>
<select type="select" name="month" id="month">
<option value="<?php echo 1; ?>">January</option>
<option value="<?php echo 2; ?>">February</option>
<option value="<?php echo 3; ?>">March</option>
<option value="<?php echo 4; ?>">April</option>
<option value="<?php echo 5; ?>">May</option>
<option value="<?php echo 6; ?>">June</option>
<option value="<?php echo 7; ?>">July</option>
<option value="<?php echo 8; ?>">August</option>
<option value="<?php echo 9; ?>">September</option>
<option value="<?php echo 10; ?>">October</option>
<option value="<?php echo 11; ?>">November</option>
<option value="<?php echo 12; ?>">December</option>
</select>
<script type="text/javascript">
document.getElementById('month').value = "<?php echo $_GET['month']; ?>";
</script>
</div>
<div>
<label for="day">Day:</label>
<select type="select" name="day" id="day">
<option value="<?php echo 1; ?>">1</option>
<option value="<?php echo 2; ?>">2</option>
<option value="<?php echo 3; ?>">3</option>
<option value="<?php echo 4; ?>">4</option>
<option value="<?php echo 5; ?>">5</option>
<option value="<?php echo 6; ?>">6</option>
<option value="<?php echo 7; ?>">7</option>
<option value="<?php echo 8; ?>">8</option>
<option value="<?php echo 9; ?>">9</option>
<option value="<?php echo 10; ?>">10</option>
<option value="<?php echo 11; ?>">11</option>
<option value="<?php echo 12; ?>">12</option>
<option value="<?php echo 13; ?>">13</option>
<option value="<?php echo 14; ?>">14</option>
<option value="<?php echo 15; ?>">15</option>
<option value="<?php echo 16; ?>">16</option>
<option value="<?php echo 17; ?>">17</option>
<option value="<?php echo 18; ?>">18</option>
<option value="<?php echo 19; ?>">19</option>
<option value="<?php echo 20; ?>">20</option>
<option value="<?php echo 21; ?>">21</option>
<option value="<?php echo 22; ?>">22</option>
<option value="<?php echo 23; ?>">23</option>
<option value="<?php echo 24; ?>">24</option>
<option value="<?php echo 25; ?>">25</option>
<option value="<?php echo 26; ?>">26</option>
<option value="<?php echo 27; ?>">27</option>
<option value="<?php echo 28; ?>">28</option>
<option value="<?php echo 29; ?>">29</option>
<option value="<?php echo 30; ?>">30</option>
<option value="<?php echo 31; ?>">31</option>
</select>
<script type="text/javascript">
document.getElementById('day').value = "<?php echo $_GET['day']; ?>";
</script>
</div>
<div>
<label for="year">Year:</label>
<select type="select" name="year" id="year">
<option value="<?= $yearBefore ?>"><?= $yearBefore ?></option>
<option value="<?= $yearCurrent ?>"><?= $yearCurrent ?></option>
<option value="<?= $year1 ?>"><?= $year1 ?></option>
<option value="<?= $year2 ?>"><?= $year2 ?></option>
<option value="<?= $year3 ?>"><?= $year3 ?></option>
<option value="<?= $year4 ?>"><?= $year4 ?></option>
</select>
<script type="text/javascript">
document.getElementById('year').value = "<?php echo $_GET['year']; ?>";
</script>
</div>
<button type="submit" name="run">Submit</button>
<div>
<p><?= $dateTimeMSG ?></p>
<p><?= $fourtyfiveMSG ?></p>
<p><?= $oneeightyMSG ?></p>
<p><?= $disclaimer1 ?></p>
<p><?= $disclaimer2 ?></p>
</div>
</form>
</div>
</div>
</body>
</html>
Not sure why you're getting an error just on the 21st but I can say your code is quite overcomplicated for what you're trying to do and you'd benefit from simplifying it. I recommend reading the PHP docs for DateTime and learning the features there. All of the validation and conversion functionality you've written here is already built into those native PHP functions, so you don't need 95% of this code, and I'll wager that eliminating it will resolve your issue. Note three important things:
If you provide an invalid date string to strtotime() or DateTime, you'll get an error -- so you don't have to explicitly check for valid values before you make those calls.
The built-in formatters like DateTime::format() will output anything you need, so you don't need to convert numbers to month names or weekdays yourself.
Relative strings like "2022-01-01 +45 days" are understood by strtotime() and DateTime. This is a super handy feature, there's no need to manually do this math.
So, pretty much all of your calculations can be replaced by:
// Given $date contains a string in YYYY-MM-DD format:
try {
$d45 = (new DateTime("$date +45 days"))->format('D, M d, Y');
$d180 = (new DateTime("$date +180 days"))->format('D, M d, Y');
} catch (Exception $e) {
// bad date provided
}
This yields:
string(17) "Tue, Feb 15, 2022"
string(17) "Thu, Jun 30, 2022"
I'd also recommend that you use <input type="date"> instead of three separate pull-downs, as it will be rendered nicely as a calendar selector in modern browsers.
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>
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'));
I select the dropdowns and nothing populates. I am not sure why? I am new to php so any help is much appreciated.
here is my code:
<section id="content" class="planner">
<select id="month" name="month">
<option value="0">--Select Month--</option>
<option value="January">January</option>
<option value="Februrary">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>
<select id="year" name="year">
<option value="0">--Select Year--</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2012">2013</option>
<option value="2012">2014</option>
<option value="2012">2015</option>
<option value="2012">2016</option>
</select>
<table class="month">
<tr class="days">
<td>Mon</td>
<td>Tues</td>
<td>Wed</td>
<td>Thurs</td>
<td>Fri</td>
<td>Sat</td>
<td>Sun</td>
</tr>
<?php
if($_POST['month'] && $_POST['year'] != 0)
{
$today = date("d"); // Current day
$month=$_POST['month'];
$year=$_POST['year'];
GenerateCalendar($today,$month,$year);
}
function GenerateCalendar($today,$month,$year)
{
$today = date("d"); // Current day
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month
$lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month
$start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month
$finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of current month
$laststart = $start - 1; // Days of previous month in calander
$counter = 1;
$nextMonthCounter = 1;
if($start > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $start) < 0){
$date = (($lastmonth - $laststart) + $counter);
$class = 'class="blur"';
}else if(($counter - $start) >= $days){
$date = ($nextMonthCounter);
$nextMonthCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $start + 1);
if($today == $counter - $start + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><a class="date">'. $date . '</a></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
}
?>
</table>
</section>
You are missing the html <form> tags around your select statements. You are also going to need an input type submit to send the form
<form action="" method="POST" name="myForm">
<!--YOUR SELECTS-->
<input type="submit" value="SEND THIS FORM"/>
</form>
the form action"" sends the post data to your current page. The method is either POST or GET. You are using POST.
That's all. You should consider learning HTML basics before continuing onto php.
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/