I want to display the dates of respective days in particular month. How can I do it? I only manage to input the month and show all the dates.
<html>
<body>
<form method="POST">
Day (Eg: Sunday) : <input name="day" required type="text" size="30" style="height:30px;" /> <br/><br/>
Month (Eg: 7) : <input name="month" required type="text" size="30" style="height:30px;" /> <br/><br/>
<input name="Submit" type="submit" value="Log In" style="background-color: #2E9AFE; border: 1px solid #084B8A; padding: 1px 3px; color:#fff; width:60px; height:30px;" />
</form>
<?php
$day=$_POST['day'];
$month=$_POST['month'];
function getDates($y, $m)
{
return new DatePeriod(
new DateTime("first sunday of $y-$m"),
DateInterval::createFromDateString('next sunday'),
new DateTime("last day of $y-$m")
);
}
foreach (getDates(2015, $month) as $getDay) {
echo $getDay->format("l, Y-m-d\n");
}
?>
</body>
</html>
If you wish to give only the month as input then iterate it with total number of days in the month
Here's the eval
<?php
$Days=array();
$Month = 8;
for($d=1; $d<=31; $d++)
{
$Time=mktime(12, 0, 0, $Month, $d, '2015');
if (date('m', $Time)==$Month)
$Days[]=date('Y-m-d-D', $Time);
}
echo '<pre>';
print_r($Days);
echo '</pre>';
?>
Note :
I have given the year directly where you can give it as dynamic (User's choice).
The Days in the month is given directly as 31 you shall find the number of the days and give that too.
Update :
As the OP wants to get only the Monday dates.
<?php
$Days=array();
$Month = 12;
for($d=1; $d<=31; $d++)
{
$Time=mktime(12, 0, 0, $Month, $d, '2014');
if (date('m', $Time)==$Month)
$Day=date('D', $Time);
if($Day=='Mon')
{
$Days[]=date('Y-m-d-D', $Time);
}
}
echo '<pre>';
print_r($Days);
echo '</pre>';
?>
Here's the updated eval
$first_date = date('Y-m-d', strtotime("first Sunday of this month", strtotime('01-07-2015')));
$last_date = date('Y-m-d', strtotime("last Sunday of this month", strtotime('01-07-2015')));
$days[] = $first_date;
while(strtotime($first_date) <= strtotime($last_date)) {
$first_date = date('Y-m-d', strtotime("next Sunday ", strtotime($first_date)));
if(strtotime($first_date) <= strtotime($last_date)) {
$days[] = $first_date;
}
}
echo '<pre>';print_r($days);exit;
Related
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 am using this function to create a dropdown for months and year my problem is am getting an error like below:
Unknown column 'format' in 'where clause'
below is the code anyone help to get rid of this error
date_condition = ' and 1=1 ';
if($date_type==1)
{
$date_condition = ' and m.submittimestamp BETWEEN DATE_SUB(NOW(), INTERVAL 300 DAY) AND NOW() ';
}
else if($date_type==2)
{
$date_condition = ' and YEAR(m.submittimestamp) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) and MONTH(m.submittimestamp)=MONTH(CURRENT_DATE - INTERVAL 1 MONTH) ';
}
else if($date_type==3)
{
$date_condition = ' and YEAR(m.submittimestamp) = YEAR(date(format)) AND MONTH(m.submittimestamp)=MONTH(date) ';
}
conditions 1 and 2 are working perfectly but condition 3 is getting error
Below is the code for drop down and script
<div class="form-group ">
<input name="filter_options"
<?php echo $option3; ?>
value="3" onchange="toggleDate(1)" type="radio" class="form-control" id="from_date">
<label for="from_date">show status for the month of </label>
<select name="month" class="form-control">
<?php
for ($i = 0; $i <= 12; ++$i) {
$time = strtotime(sprintf('-%d months', $i));
$value = date('m', $time);
$label = date('F ', $time);
printf('<option value="%s">%s</option>', $value, $label);
}
?>
</select>
<select name="year" class="form-control">
<?php
for ($i = 0; $i <= 12; ++$i) {
$time = strtotime(sprintf('-%d years', $i));
$value = date('Y', $time);
$label = date('Y ', $time);
printf('<option value="%s">%s</option>', $value, $label);
}
?>
<option value=" ">year</option>
</select>
</div>
The error is telling you everything you need to know, you have a syntax error. Remove the letter "e" at the beginning of the code noted in your error.
e) AND MONTH(m.submittimestamp)=MONTH(date) GROUP
should be
) AND MONTH(m.submittimestamp)=MONTH(date) GROUP
I don't have a background in PHP but a recent client requires some updating so i'm doing my best. What I'm trying to accomplish is first update the form to accept the correct date format to covert using the code I created that will through an error if it's less than 24 hours from the present time. The form now:
<p>
<label for="date">Date:<span>*</span></label>
<span>
<input type="text" name="date" id="date" value="<?php $this->value('date'); ?>" size="25" />
</span>
</p>
<p>
<label for="time">Eat Time:<span>*</span></label>
<span>
<input type="text" name="time" id="time" value="<?php $this->value('time'); ?>" size="10" />
</span>
</p>
<p>
<label for="timedes">AM or PM:<span>*</span></label>
<span>
<select name="timedes" id="timedes">
<option value="0" <?php $this->value('timedes', '0', 'selected="selected"'); ?>>Select..</option>
<option value="AM" <?php $this->value('timedes', 'AM', 'selected="selected"'); ?>>AM</option>
<option value="PM" <?php $this->value('timedes', 'PM', 'selected="selected"'); ?>>PM</option>
</select>
</span>
</p>
The code that I will need to modify to validate if it's less than 24 hours or not.
// define variables for date
date_default_timezone_set('America/New_York'); // timezone
$dateInput = '10/11/2015 15:54'; //date they selected string
$cateringDate = (strtotime("$dateInput")); // date selected converted to time
$dateNow = date('m/d/y H:i'); // current date and time string
$currenttime = (strtotime("$dateNow")); // current date and time converted
$dateAllowed = $currenttime + (86400); // current date and time 24 hours added on
$difference = $dateAllowed - $cateringDate; // date selected vs current date + 24hours
if ($cateringDate >= $dateAllowed) {
echo 'good job';
} else {
echo 'try again';
}
There are multiple ways to do this. But here is one using strtotime:
date_default_timezone_set('America/New_York'); // timezone
$dateInput = '10/11/2015 15:54';
if (strtotime('+24 hours') < strtotime($dateInput)) {
echo 'good job';
} else {
echo 'try again';
}
If strtotime did not understand the format it returns false, so:
if (false === strtotime($dateInput)) {
echo 'Sorry, what?';
}
Use the datetime class and take advantage of the diff method, i.e.:
date_default_timezone_set( 'America/New_York' ); // timezone
$date1 = new DateTime( 'NOW' );
$dateInput = '10/10/2015 15:54';
$date2 = DateTime::createFromFormat( 'm/d/Y H:i', $dateInput );
$diff = $date2->diff( $date1 );
if( $diff->format( '%d' ) != 0 ){
echo "good job";
}else{
echo "try again";
}
We check if the difference (in days) between NOW and the $dateInput is different from 0, zero means less than 24h from a future or previous date.
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'));
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Get next/previous ISO week and year in PHP
I am trying to write a script that will display the days of a week in a table and that will advance a week if a button is clicked. I have managed to get it working up until the point where it reaches the end of the year and then the dates all go wrong. He is what I have so far...
<?
if(isset($_POST['add_week'])){
$week = date('d-m-Y', strtotime($_POST['last_week']));
$new_week = strtotime ( '+1 week' , strtotime ( $week ) ) ;
$new_week = date('d-m-Y', $new_week);
$week_number = date("W", strtotime( $new_week));
$year = date("Y", strtotime( $new_week));
}else{
$week_number = date("W");
$year = date("Y");
}
if($week_number < 10){
$week_number = "0".$week_number;
}
$week_start = date('d-m-Y', strtotime($year."W".$week_number,0));
echo $week.' '.$new_week.' '.$week_number;
?>
<table name="week">
<tr>
<?
for($day=1; $day<=7; $day++)
{
echo '<td>';
echo date('d-m-Y', strtotime($year."W".$week_number.$day))." | \n";
echo '</td>';
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" />
<input ype="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>
Some of the values have been echo'd so I can check the values that are being passed are correct and I know I have probably taken extra steps I didn't need to but I am fairly new to this and wanted to make the code easier to folllow whilst I get it working. As I said, the add button works a treat until it hits new year.
Thanks
Ok, made some advancement, works fine until it gets to 2012 then it just runs through 2012 again rather than starting 2013
<?
if(isset($_POST['add_week'])){
$week = date('d-m-Y', strtotime($_POST['last_week']));
$new_week = strtotime ( '+1 week' , strtotime ( $week ) ) ;
$new_week = date('d-m-Y', $new_week);
$week_number = date("W", strtotime( $new_week));
$year = date("Y", strtotime( $new_week));
}else if(isset($_POST['back_week'])){
$week = date('d-m-Y', strtotime($_POST['last_week']));
$new_week = strtotime ( '-1 week' , strtotime ( $week ) ) ;
$new_week = date('d-m-Y', $new_week);
$week_number = date("W", strtotime( $new_week));
$year = date("Y", strtotime( $new_week));
}else{
$week_number = date("W");
$year = date("Y");
}
/*if($week_number < 10){
$week_number = "0".$week_number;
}*/
$week_start = date('d-m-Y', strtotime($year."W".$week_number,0));
echo $week.' '.$new_week.' '.$week_number;
?>
<table name="week">
<tr>
<?
for($day=1; $day<=7; $day++)
{
echo '<td>';
echo date('d-m-Y', strtotime($year."W".$week_number.$day))." | \n";
echo '</td>';
}
?>
</tr>
<tr>
<form name="move_weeks" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="last_week" value="<? echo $week_start; ?>" />
<td colspan="7"><input type="submit" name="back_week" value="back_week" /><input type="submit" name="add_week" value="add_week" />
</td>
</form>
</tr>
</table>
In my opinion you are going to be way better served to make all your calculation based on a unix timestamp value and then convert to string only as needed for output. That way you don't have to deal with week number problems (i.e. week 0), you are not limited to having Monday be the first day of each week (as as is the basis of calculation in date("W")), and you won't have to make a bunch of hacks to look for edge conditions.
So assuming that $_POST['last_week'] is in your d-m-Y format something like this:
if(isset($_POST['add_week'])){
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts + (3600 * 24 * 7);
} else if (isset($_POST['back_week'])) {
$last_week_ts = strtotime($_POST['last_week']);
$display_week_ts = $last_week_ts - (3600 * 24 * 7);
} else {
$display_week_ts = floor(time() / (3600 * 24)) * 3600 * 24;
}
$week_start = date('d-m-Y', $display_week_ts);
For the part where you are looping through the week to display you can use something like this:
for ($i = 0; $i < 7; $i++) {
$current_day_ts = $display_week_ts + ($i * 3600 *24);
echo date('d-m-Y', $current_day_ts);
}