I want to draw a perpetual calendar on 7 following day
$dt = new DateTime;
if (isset($_POST['annee']) && isset($_POST['semaine'])) {
$dt->setISODate($_POST['annee'], $_POST['semaine']);
} else {
$dt->setISODate($dt->format('o'), $dt->format('W'));
};
$annee= $dt->format('o');
$semaine= $dt->format('W');
$KlendrierPerpetuel="";
$KlendrierPerpetuel.='<table><tr>';
do {
$KlendrierPerpetuel.='<td>'.$dt->format('D').'</td>';
$dt->modify('+1 day');
} while ($semaine == $dt->format('W'));
$KlendrierPerpetuel.='</tr></table>';
echo $KlendrierPerpetuel;
it creates a table width day of the week instead of 7 next day
Well... it's actually a lot more simple...
$format = "y/m/d";
$date_arr = [];
for($i = 0; $i < 6; $i ++){
if($i = 0)
$date_arr[] = date($format);
else
$date_arr[] = date($format, strtotime("+$i day");
}
strtotime can parse date from a string like "+1 day" or "+1 week", so have the next day is quite simple. So you just need to loop the process and it's done.
PS : In your initial post, you seem to get some post parameters, you just need to put them in a strtotime() in the if.
What you're looking for isn't completely clear, but I took my best guess and added user controls to implement it. You should be able to modify this to suit your needs:
<html>
<form method='post'>
<input type='numeric' name='year' value='<?= $_POST['year'] ?: date('Y') ?>'>
<input type='numeric' name='week' value='<?= $_POST['week'] ?: date('W') ?>'>
<input type='submit'>
</form>
<?php
$dt = new DateTime;
if (isset($_POST['year']) && isset($_POST['week'])) {
$dt->setISODate($_POST['year'], $_POST['week']);
} else {
$dt->setISODate($dt->format('o'), $dt->format('W'));
};
$year= $dt->format('o');
$week= $dt->format('W');
$KlendrierPerpetuel="";
$KlendrierPerpetuel.='<table><tr><td>'.$dt->format('M').'</td>';
do {
$KlendrierPerpetuel.='<td>'.$dt->format('d').'<br>'.$dt->format('D').'</td>';
$dt->modify('+1 day');
} while ($week == $dt->format('W'));
$KlendrierPerpetuel.='<td>'.$dt->format('M').'</td></tr></table>';
echo $KlendrierPerpetuel;
This allows user specification of the year and week number, and displays the days of that week.
Related
Anyone know, how I can achieve below:
If someone select the option "For weekend only" then all the weekend dates will be selected from the current month to the next month's specified date.
if someone select the option "For weekday only" then all the weekday dates will be selected from the current month to the next month's specified date.
if someone select the option "For Friday only" then all the Friday dates will be selected from the current month to the next month's specified date.
Something like above:
Anyone have any idea where I can start this with??
$('#dt_1').datepicker({
rtl: KTUtil.isRTL(),
todayHighlight: true,
templates: arrows,
startDate: date, //disable all old dates
setDate: date, //tomorrow's date allowed
multidate: true,
format: 'dd/mm/yyyy'
});
I've found the solution to achieve this with the help of another post of stackoverflow:
get weekend using php
PHP code:
//weekend.....
$now = strtotime("now");
$end_date = strtotime("+3 weeks");
$weekend_array = array();
while (date("d-m-Y", $now) != date("d-m-Y", $end_date)) {
$day_index = date("w", $now);
if ($day_index == 0 || $day_index == 6) {
// Print or store the weekends here.
//echo "<br>".$day_index."- Date: ".date("d-m-Y", $now);
$weekend_array[] = date("d-m-Y", $now);
}
$now = strtotime(date("d-m-Y", $now) . "+1 day");
}
//friday only....
$now = strtotime("now");
$end_date = strtotime("+3 weeks");
$friday_array = array();
while (date("d-m-Y", $now) != date("d-m-Y", $end_date)) {
$day_index = date("w", $now);
if ($day_index == 5) {
// Print or store the weekends here.
//echo "<br>".$day_index."- Date: ".date("d-m-Y", $now);
$friday_array[] = date("d-m-Y", $now);
}
$now = strtotime(date("d-m-Y", $now) . "+1 day");
}
//weekdays......
$now = strtotime("now");
$end_date = strtotime("+3 weeks");
$weekday_array = array();
while (date("d-m-Y", $now) != date("d-m-Y", $end_date)) {
$day_index = date("w", $now);
if ($day_index >= 1 && $day_index <= 5) {
// Print or store the weekends here.
//echo "<br>".$day_index."- Date: ".date("d-m-Y", $now);
$weekday_array[] = date("d-m-Y", $now);
}
$now = strtotime(date("d-m-Y", $now) . "+1 day");
}
Javascript code:
$(document).ready( function () {
$("#sel_dropdown").change(function() {
$( "select option:selected" ).each(function() {
var v = $(this).val();
if(v == 1) {
//weekend only....
$('#dt_1').datepicker('setDates',[<?php
$i=0;
for($i=0;$i<=count($weekend_array);$i++) {
echo "'".$weekend_array[$i]."',";
}
?>]);
}
if(v == 2) {
//Friday only.....
$('#dt_1').datepicker('setDates',[<?php
$i=0;
for($i=0;$i<=count($friday_array);$i++) {
echo "'".$friday_array[$i]."',";
}
?>]);
}
if(v == 3) {
//Weekdays only....
$('#dt_1').datepicker('setDates',[<?php
$i=0;
for($i=0;$i<=count($weekday_array);$i++) {
echo "'".$weekday_array[$i]."',";
}
?>]);
}
if(v == 4) {
//custom option....
$('#dt_1').data('datepicker').setDate(null);
}
});
});
});
</script>
Hopefully, this will useful for anyone looking for this type of solution.
I am working on a "cut-off" date and I need to set it on every month. Say, I set the cut-off date to August 25 for this year, then it should be September 25, October 25 and so on till the year ends.
Here's the code I have:
$now = "2015-08-25";
$nextDate = getCutoffDate($now);
echo $nextDate;
function getCutoffDate($start_date)
{
$date_array = explode("-",$start_date); // split the array
// var_dump($date_array);
$year = $date_array[0];
$month = $date_array[1];
$day = $date_array[2];
/*if (date('n', $now)==12)
{
return date("Y-m-d",strtotime(date("Y-m-d", $start_date) . "+1 month"));
}*/
if (date("d") <= $day) {
$billMonth = date_format(date_create(), 'm');
}
else{
$billMonth = date_format(date_modify(date_create(), 'first day of next month'), 'm');
}
// echo date("d").' '. $billMonth.'<br>';
$billMonthDays = cal_days_in_month(CAL_GREGORIAN, ($billMonth), date("Y"));
// echo $billMonthDays.'<br>';
if ($billMonthDays > $day) {
$billDay = $day;
} else {
$billDay = $billMonthDays;
}
}
I got this from here: http://www.midwesternmac.com/blogs/jeff-geerling/php-calculating-monthly
It returns the same date for the next month only, but how do I get the specific date of EACH month of the current year? Kindly leave your thoughts. Still a newbie here, sorry.
In that case, this should be enough:
<?php
for($i=8; $i<=12; $i++) {
echo sprintf('25-%02d-2015', $i) . '<br>';
}
but if you need more flexible way:
<?php
$date = new DateTime('25-08-2015');
function getCutoffDate($date) {
$days = cal_days_in_month(CAL_GREGORIAN, $date->format('n'), $date->format('Y'));
$date->add(new DateInterval('P' . $days .'D'));
return $date;
}
for($i = 0; $i < 5; $i++) {
$date = getCutoffDate($date);
echo $date->format('d-m-Y') . '<br>';
}
This should print:
25-09-2015
25-10-2015
25-11-2015
25-12-2015
25-01-2016
I have this code
<?php
$startYear = 2011;
while ($startYear <= date('Y')) {
echo "$startYear <br>";
for ($m=1; $m<=12; $m++) {
$theDate = $startYear . $m;
if ($theDate <= date('Yn')) {
$month = date('F', mktime(0,0,0,$m, 1, $startYear));
echo $month. '<br>';
}
}
$startYear++;
};
?>
because I would like to write a list of the type
2013
January
February
March
April
May
June
July
August
September
October
November
December
until the current year and the present month
but every list of months of every year just goes until september, why?
You have to change your if statement from this:
if ($theDate <= date('Yn')) {
to this:
if ($theDate <= date('Ym')) {
//^ See here the placeholder for month with leading zero
For more information about date() see the manual: http://php.net/manual/en/function.date.php
Because 201410 > 21049. This is what happens when you use date() for date math for which it is not suited.
DateTime() and its related functions are better suited for this:
$startYear = 2011;
$start = (new DateTime())->setDate($startYear,1,1);
$end = (new DateTime())->setDate($startYear,1,1)->modify('+1 year');
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format('F') . "\n";
}
Demo
If you're using PHP 5.5+ it is a little cleaner
$startYear = 2011;
$start = (new DateTimeImmutable())->setDate($startYear,1,1);
$end = $start->modify('+1 year');
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format('F') . "\n";
}
Demo
i edited my code to add a leading zero to the variable who will represent the month number, edited the current date from date('Yn') to date('Ym') and works.
<?php
$startYear = 2011;
while ($startYear <= date('Y')) {
echo "$startYear <br>";
for ($m=1; $m<=12; $m++) {
$f = sprintf('%02d', $m);
$theDate = $startYear . $f;
if ($theDate <= date('Ym')) {
$month = date('F', mktime(0,0,0,$f, 1, $startYear));
echo $month. '<br>';
}
}
$startYear++;
};
?>
Thanks for the answer guys.
I made a simple PHP script for generating calendar. It has rounded weeks (I mean if month starts on Friday it will generate it monday of that week), navigation, etc. Works great, but there is a bug in October. It draws last sunday of month twice. I use sundays as a singnal for new row so it makes
Example of my calendar
Example of calendar with October bug
And here the code (I'm not professional, I learned PHP on my own, with no books, just with google and PHP manual):
<?php
function getfirstday($month, $year)
{
$datestr = "01-$month-$year";
$day = date('N', strtotime($datestr));
return $day;
}
function getlastday($month, $year)
{
$datestr = cal_days_in_month(CAL_GREGORIAN, $month, $year)."-$month-$year";
$day = date('N', strtotime($datestr));
return $day;
}
//Don't care about this, I just want to have weekdays in my primary language
function getweekday($weekDay) {
$list = array();
$list['1'] = "Pondělí";
$list['2'] = "Úterý";
$list['3'] = "Středa";
$list['4'] = "Čtvrtek";
$list['5'] = "Pátek";
$list['6'] = "Sobota";
$list['7'] = "Neděle";
return $list[$weekDay];
}
//What month and year do we want to show?
//Ger it from URL or use current
$month = $_GET['month'];
if ($month == "") {
$month = date('m');
}
$year = $_GET['year'];
if ($year == "") {
$year = date('Y');
}
//Firts day of month
$startDayStr = "01-$month-$year";
//Some calculation to get interval of whole month and rounded weeks
$startDay = strtotime($startDayStr) - (getfirstday($month, $year ) - 1) * 24*60*60;
$roundMonth = 7 - getlastday($month, $year );
$limit = cal_days_in_month(CAL_GREGORIAN, $month, $year ) + (getfirstday($month, $year ) - 1) + $roundMonth;
//Some navigation
?>
<table>
<tr>
<?php
if ($month > 1) {
$prevm = $month - 1;
$prevh = "cal.php?month=$prevm&year=$year";
} elseif ($month == 1) {
$prevm = 12;
$prevy = $year -1;
$prevh = "cal.php?month=$prevm&year=$prevy";
}
if ($month < 12) {
$nextm = $month +1;
$nexth = "cal.php?month=$nextm&year=$year";
} elseif ($month == 12) {
$nextm = 1;
$nexty = $year +1;
$nexth = "cal.php?month=$nextm&year=$nexty";
}
?>
<td width="200" align="left"><Previous month</td>
<td width="190" align="center">
<?php echo date('m', strtotime("01-$month-$year 00:00:00")); ?>
</td>
<td width="200" align="right">Next month></td>
</tr>
</table>
<?php
//Let's generate our calendar
echo "<table border=\"1\"><tr>";
for($j=1;$j<=7;$j++){
echo "<td align=\"center\" width=\"85\" height=\"50\"><b>".getweekday($j)."</b></td>";
}
echo "</tr><tr>";
for($i = 1;$i <= $limit;$i++) {
$lastDayOfMonth = strtotime(cal_days_in_month(CAL_GREGORIAN, $month, $year)."-$month-$year 23:59:59");
$firstDayOfMonth = strtotime("01-$month-$year");
$weekDay = date('N', $startDay);
if ($startDay < $firstDayOfMonth || $startDay > $lastDayOfMonth) {
$class = "caltdb";
} else {
$class = "caltda";
}
echo "<td class=\"$class\" align=\"center\" height=\"40\">". date('d', $startDay) ."</td>";
if ($weekDay == '7') {
echo "</tr><tr>";
}
$startDay = $startDay + 24*60*60;
}
echo "</tr></table>";
?>
Could you help me fix this problem? I dont know why is it happening.
Thank you a lot,
Heretiiik
The problem is likely due to the fact that you're using + 24*60*60 to add one day to a timestamp. This causes problems with daylight saving time, because there are days with 23 or 25 hours when DST begins/ends.
Near the end of your script, replace:
$startDay = $startDay + 24*60*60;
with:
$startDay = strtotime('+1 day', $startDay);
I have some DB entries, they have timestamps. And all I want is to draw a separate line between days. And also, I need the day to start not at 00:00, but at 07:00. It's like an offset for the day start.
Now I have that (for context):
foreach($logs as $log) {
$cur_date = $log[0]['timestamp'];
echo "<p>".$log[0]['content']."</p>";
}
Is there a simple workaround for the problem I've described?
Thank you!
$prev_date = 0;
foreach($logs as $log) {
$cur_date = strtotime($log[0]['timestamp']);
$cur_day_beginning = strtotime(date("Y-m-d 07:00:00", $cur_date));
if ($cur_date >= $cur_day_beginning && $prev_date < $cur_day_beginning) {
echo "<hr/>";
}
$prev_date = $cur_date;
echo "<p>".$log[0]['content']."</p>";
}