RESOLVED in answer below
Creating a scheduling tool for my users and I am having an issue trying to increment my variable obtained from a dropdown selection:
$date_select = $_POST['date_select'];
I'm sure it's a textbook fix, but to put it simply...I need to increment $date_select by +1 week for 52 weeks.
I have a dropdown menu starting with the current date, and looping to the end of 365 days, incrementing by 1. No problem here.
<select name="date_select" form="create_schedule">
<?php
for($i = 0; $i <= 365; $i++){
$d=strtotime($i . " Day");
$day = date("n-j-y l", $d) . "<br>";
echo "<option>" . $day . "</option>";
}
?>
</select>
This selection is represented by:
$date_select = $_POST['date_select'];
Next to that, before submitting, users can select a radio button - either M, T, W, Th, F, Sat, or Sun to indicate if they would like to apply their request to that selected day, for every week, for the rest of the year. (Which is what I'm trying to do...which is: increment $date_select by "+1 Week" until the for loop is finished)
This selection is represented by:
$repeat = $_POST['repeat'];
This is the closest I've gotten...the code below increments for every "Monday" like I want for example...if $repeat == 'M', but the numerical dates are wrong...
if(isset($_POST['repeat'])){
for($i = 0; $i <= 52; $i++){
$date = strtotime($i . " week", strtotime($date_select));
echo date("n-j-y l", $date) . "<br/>";
}
For example: if the date selected is 7-4-16 Monday, the output is this:
11-26-07 Monday
12-3-07 Monday
12-10-07 Monday
12-17-07 Monday
12-24-07 Monday
12-31-07 Monday
1-7-08 Monday
And so forth...
Thank you in advance.
RESOLVED The issue was in the date format..."m-d-Y" is not equivalent to "m/d/Y" when incrementing days weeks or months in regards to how it is output. Somewhere along the lines, "American" date format values and "European" date format values were getting mixed up. I changed the date format within both of the for-loops and got it working.
"Note: Be aware of dates in the m/d/y or d-m-y formats; if the separator is a slash (/), then the American m/d/y is assumed. If the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential errors, you should YYYY-MM-DD dates or date_create_from_format() when possible."
http://www.w3schools.com/php/func_date_strtotime.asp
Here is the working solution in case anyone is trying to do something similar
<select name="date_select" form="create_schedule">
<?php
for($i = 0; $i <= 365; $i++){
$d=strtotime($i . " Day");
$day = date("m/d/Y l", $d) . "<br>";
echo "<option>" . $day . "</option>";
}
?>
</select>
if(isset($_POST['repeat'])){
$repeat = $_POST['repeat'];
echo "<br/>";
for($i = 0; $i <= 13; $i++){
$d=strtotime($i . " week", strtotime($date_select));
echo date("m/d/Y l", $d) . "<br/>";
}
}
So on PHP you have to classes that can be really helpfull doing that
\DateTime and \DateInterval
So to do what you want I would recommend
$firstDate = \DateTime::createFromFormat('Y-m-d', $date_select));
$baseDate = clone $firstDate;
$intervalToAdd = new \DateInterval('P1w')
if(isset($_POST['repeat'])){
for($i = 0; $i <= 52; $i++){
$date [$i] = $baseDate->add($intervalToAdd);
echo '<option>'.$date[$i]->format('Y-m-d').'</option>';
}
Related
I need to display date limited in SELECT tag HTML by loop php and wordpress .
only need to display four days later.
like sample image :
Also, for hours and minutes this way.
thanks .
get today
$date = date('Y-m-d');
you can loop over this to get the next days
$next_dates = date('DD', strtotime($date .' +1 day'));
loop example
for ($x = 0; $x <= 10; $x++) {
$next_date = date('DD', strtotime($date .' +1 day'));
echo "The next is: $next_date <br>";
}
you can put your data then in <select> tag as <option>
Please, i need assistance in this code.I have checked others in Stakeoverflow, but it is not combatible, hence this question. I want to generate all working /weekdays between two dates.I have found a code, but it is generating all days, including weekend. How do i eliminate the weekend from the list or ensure the list generated is ONLY for weekdays?
<?php
$start_Date = date('Y-m-d');
$end_Date = date('Y-m-d', strtotime('30 weekdays'));
//echo $start_Date."<br/>";
//echo $end_Date."<br/>";
// Specify the start date. This date can be any English textual format
$date_from = $start_Date;
$date_from = strtotime($date_from); // Convert date to a UNIX timestamp
// Specify the end date. This date can be any English textual format
$date_to = $end_Date;
$date_to = strtotime($date_to); // Convert date to a UNIX timestamp
// Loop from the start date to end date and output all dates inbetween
$c = 0;
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
I expect 30days to be generated but with this code, I am getting 42days . Weekend has been added,instead of weekdays ONLY .
Just add this to your loop:
$w = date('w',$i);// day of week - Sunday == 0, Saturday == 6
if($w == 0 || $w == 6){
continue;
}
DEMO
Your code is almost working only have to add a if checking in your code
your code
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
please replace with that one
for ($i = $date_from; $i <= $date_to; $i += 86400) {
$day = date("w", $i);
if($day != 0 && $day!= 6){ // will continue if not Sunday or Saturday
$c++;
echo $c . "=> " . date("Y-m-d", $i) . '<br />';
}
}
You also can take help from php.net
Thanks
You may need to get the day of the week, like date("D"), then use it in your for loop to check..something like this?:
$Weekends = array("Sat","Sun");
for....
$DayOfWeek = date("D",$i);
if(!in_array($DayOfWeek, $Weekend)){
// increment...
}
I can display dates from start to end date from stored data in mysql, but I want to display current month dates from 1st to end date of this month in form of
1
2
3
4
.
.
.
.
.
31
Is this possible?
Refer to PHP cal_days_in_month
As explained here
This function will return the number of days in the month of year for the specified calendar.
int cal_days_in_month ( int $calendar , int $month , int $year )
And an example:
$number = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
echo "There were {$number} days in August 2003";
Use a loop to display a count of the number of days
For the PHP part, this might help you:
// Get the current date
$today = getdate();
// Get the number of days in current month
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $today['mon'], $today['year']);
// Print the dates
for ($i = 1; $i <= $days_in_month; $i++) {
echo ' ' . $i;
}
Styling and output is another task, this is just to get you started.
yes. it is possible.
please, use below php code. it can work for php 4.1 and higher.
<?php
$number = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
for($i=1;$i<=$number;$i++)
echo $i.'<br>';
?>
If you want all days in the month, try this loop where date("t") give you the numerical last day of the month, and we know the first day is always 1.
$last = date("t");
for($i=1; $i<= $last; $i++) echo "$i ";
I want to insert dynamic months into database like if user select March then I need to insert record for 12 months from March to Feburary. I am getting dynamic months but when I am trying to insert it into database it insert only first 12 months. I need to repeat the loop again from March to February if user click on add more button. This is my code :
$months = array();
$date="august";
$year= '2014';
//$y= (int)$year;
$currentMonth= date('m', strtotime($date));
$currentyear= date('Y', strtotime('+1 year'));
for($x = $currentMonth; $x < $currentMonth+12; $x++)
{
$months[] = date('F Y', mktime(0, 0, $currentyear, $x,1));
}
//print_r($months);
for($i=0; $i<=23 ; $i++)
{
echo $insert= "insert into month(month_name) values('".$months[$i]."')";
}
as your months array only has 12 values, you can't go to value 23 in that array. What you can do is run through the array twice from 0 to 11, like this:
for($j=0; $j<2 ; $j++)
{
for($i=0; $i<12 ; $i++)
{
echo $insert= "insert into month(month_name) values('".$months[$i]."')";
}
}
or as clyde indicated you can use a modulo operator, which doesn't make you waste 2 loops and thus is faster:
for($i=0; $i<24 ; $i++)
{
echo $insert= "insert into month(month_name) values('".$months[$i % 12]."')";
}
Maybe you can use the DateTime object to do this.
$string = '01-08-2013'; //input string from user
$StartDate = new DateTime($string);
$StopDate = new DateTime($string);
$StopDate->modify('+1 year');
while($StartDate < $StopDate) { //loop as long as $StartDate is smaller than $StopDate
echo "inserting " . $StartDate->format('d/m/Y') . ' into database ' . "<br/>";
//execute mysql query;
$StartDate->modify('+1 month');
}
Your question should state why you want this series of months in the database, as nobody normally does so. Normally people put timestamps on transactions/event records into a database and then report on them.
I need help Select every other Wednesday starting on 5/2/12. This code below selects every other Wednesday starting on the week it currently is. But i need to set the beginning week. I am familiar with PHP, but not familiar with php dates. So please be as specific as possible.
I found this:
$number_of_dates = 10;
for ($i = 0; $i < $number_of_dates; $i++) {
echo date('m-d-Y', strtotime('Wednesday +' . ($i * 2) . ' weeks')). "<br>".PHP_EOL;
}
Use mktime to create your starting date and pass that as the second argument to strtotime so that counting starts from there:
$startDate = mktime(0, 0, 0, 5, 2, 2012); // May 2, 2012
for ($i = 0; $i < $number_of_dates; $i++) {
$date = strtotime('Wednesday +' . ($i * 2) . ' weeks', $startDate);
echo date('m-d-Y', $date). "<br>".PHP_EOL;
}
See it in action.
Give it a date in the string, instead of "Wednesday" (that chooses the next Wednesday), write:
strtotime('20120502 +' . ($i * 2) . ' weeks'))
To choose that date. (Format is yyyymmdd).
If you have PHP 5.2.0 or newer, you can do it easily this way:
$date = new DateTime('2006-05-02');
for ($i=0; $i<10; $i++) {
echo $date->format('m-d-Y').'<br/>'.PHP_EOL;
$date->modify('+1 week');
}
You could also use the DatePeriod and DateInterval classes to make life easier.
Standard disclaimer: both of the classes above require PHP >= 5.3.0.
$number_of_dates = 10;
$start_date = new DateTime("5/2/12");
$interval = DateInterval::createFromDateString("second wednesday");
$period = new DatePeriod($start_date, $interval, $number_of_dates - 1);
foreach ($period as $date) {
echo $date->format("m-d-Y") . "<br>" . PHP_EOL;
}