Php.Advance weekly calendar one week [duplicate] - php

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);
}

Related

PHP post - converting time in form

i want to build a site, where a user can choose a timeslot. This timeslot should be printed on the following site. But i have a problem with the correct presentation or displaying the chosen time.
Here my code so far:
Site1, where the user choose a timeslot:
<form name="Uhrzeit" action="confirmation.php" method="POST">
<?php
$start = 10;
$end = 18;
for ($time = $start; $time <= $end; $time++) {
echo "<input type='radio' name='Zeit'>";
echo date("H:00", mktime($time+0,25)).'<br>';
}
?>
<input type="submit" value="Wählen">
</form>
Site2, where the chosen time should be printed:
<?php
$new_date = date('H:00', strtotime($_POST['Zeit']));
echo $new_date;
?>
But with this i get everytime the output of "01:00".
Please try below code
<form name="Uhrzeit" action="confirmation.php" method="POST">
<?php
$start = 10;
$end = 18;
for ($time = $start; $time <= $end; $time++) {
echo "<input type='radio' name='Zeit' value='".date("H:00", mktime($time+0,25))."'>";
echo date("H:00", mktime($time+0,25)).'<br>';
}
?>
<input type="submit" value="Wählen">
</form>

create drop down to view month and year in php

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

How can I get the days of the month by using php?

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;

Getting previous months in 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'));

Populate checkbox from array

Hi I've data for year store as serialize like this
a:2:{i:0;s:4:"2011";i:1;s:4:"2013";}
and have a list of predefined year like this:
$current_year = date('Y');for($year = 2011; $year < $current_year; $year++) { $year;}
So, I want to populate a list of checkboxes like below:
[x] 2011
[ ] 2012
[x] 2013
...
If the year is not in the predefined year (in this case 2011,2012 and 2013), the year should be unchecked.
I've search but so far the nearest solution is not in PHP
$years = unserialize($mysql_years);
$current_year = date('Y');
for ($year = 2011; $year < $current_year; $year++) {
$checked = '';
if (in_array($year, $years)) {
$checked = ' checked';
}
echo "<input type=checkbox value=$year$checked>";
}
I have found the solution. Check this.
<?php
$dat='a:2:{i:0;s:4:"2011";i:1;s:4:"2013";}';
$data=unserialize($dat);
$current_year = date('Y');
for($year = 2011; $year <= $current_year; $year++)
{
if(in_array($year,$data)) { $checked="CHECKED"; } else { $checked=""; } ?>
<input type="checkbox" value="<?php echo $year; ?>" name="year[]" <?php echo $checked; ?> > <?php echo $year; ?> <br/>
<?php } ?>

Categories