I have an event calendar that starts on Sunday. I must change it to start on Monday.
Part of my code:
<html>
<body>
<?php
$dagteller=$firstDayArray["wday"];
$mDay=$firstDayArray["mday"];
define("ADAY", (60*60*24));
$mydate=getdate(date("U"));
define("ADAY", (60*60*24));
for ($count=0; $count < (6*7); $count++) {
$dayArray = getdate($start);
if (($count % 7) == 0) {
if ($dayArray["mon"] != $month) {
break;
} else {
echo ("</tr ><tr>\n");
}
}
if ((!isset($_POST['month'])) || (!isset($_POST['year']))) {
$nowArray = getdate();
$month = $nowArray['mon'];
$year = $nowArray['year'];
$day = $nowArray['day'];
} else {
$month = $_POST['month'];
$year = $_POST['year'];
}
// on my table
echo ("<td bgcolor=\"#DDDDDD\"><center>".$dayArray["mday"]."</center></td>\n");
$start += ADAY;
?>
</body>
</html>
Read the PHP date manual for yourself:
http://www.php.net/manual/en/function.date.php
The 'W' identifier will help you
Related
Im trying to write php code which would start counting weeks from specific month. For me its September and February. For example desired result for 01.09.2017 would be Semester-1,Week-1. and for 04.09.2017 would be Semester-1,Week-2. I found similar topics here and here . But their output result is array, should i work with arreys here too ? I want to mention that I almost have zero expierence with php language.
This is what I have come up with so far:
<?php
$day = date("D");
$month = date("M");
if($month == 'Apr'||'Feb'||'Mar'||'May') {
print "Semester-2,";
}
else print "";
if($month == 'Sep'||'Oct'||'Nov'||'Dec') {
print "Semester-1,";
}
else print "";
if($month == 'Jan') {
print "Exams";
}
if($month == 'Jun') {
print "Exams,";
}
if($month == 'Jun') {
print "Exams,";
}
if($month == 'Jul'||'Aug') {
print "Summer Break,";
}
You could do something like this:
$month = date('n'); // Month number 1-12
if ($month >= 9 && $month <=12) {
$period = 'Semester-1';
$startWeek = date('W', strtotime(date('Y') . '-09-01'));
} elseif ($month >= 2 && $month <=5) {
$period = 'Semester-2';
$startWeek = date('W', strtotime(date('Y') . '-02-01'));
} elseif ($month == 1) {
$period = 'Exams';
$startWeek = date('W', strtotime(date('Y') . '-01-01'));
} elseif ($month == 6) {
$period = 'Exams';
$startWeek = date('W', strtotime(date('Y') . '-06-01'));
} elseif ($month == 7 || $month == 8) {
$period = 'Summer break';
$startWeek = date('W', strtotime(date('Y') . '-07-01'));
}
$currentWeek = date('W') - $startWeek + 1;
echo $period . ' ' . 'Week-' . $currentWeek;
After #Qirel comment, I thought of something like this, hope this helps :
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$month = date("M"); /* current month */
if (in_array($month, array("Sep", "Oct", "Nov", "Dec"))) { $myperiod = "Semester #1"; }
if (in_array($month, array("Feb", "Mar", "Apr", "May"))) { $myperiod = "Semester #2"; }
if($month == 'Jan') { $myperiod = "Mid-Exams #1"; }
if($month == 'Jun') { $myperiod = "Final-Exams #2"; }
if( ($month == 'Jul') || ($month == 'Aug') ) { $myperiod = "Summer break"; }
$today = date("Y-m-d"); /* or use your date from user data */
$date = new DateTime($today);
$week = $date->format("W"); /* use of PHP function 'date' to get week # */
$currentweek = "$week";
echo "[ Week # $week ]";
echo"You currently are in : $myperiod - $currentweek";
?>
This should give you what you need:
$semesters = array(
'Sep' => 'Semester-1',
'Oct' => 'Semester-1',
'Nov' => 'Semester-1',
'Dec' => 'Semester-1',
'Jan' => 'Exams',
'Feb' => 'Semester-2',
'Mar' => 'Semester-2',
'Apr' => 'Semester-2',
'May' => 'Semester-2',
'Jun' => 'Exams',
'Jul' => 'Summer Break',
'Aug' => 'Summer Break',
);
switch ($semesters[date('M')]) {
case 'Semester-1':
$sep1st = strtotime('2017-09-01');
$week1 = date('W', $sep1st);
$currentWeek = date('W');
echo 'Semester-1, Week-', $currentWeek - $week1 + 1; // +1 because the count starts at 1.
break;
case 'Semester-2':
$feb1st = strtotime('2018-02-01');
$week1 = date('W', $feb1st);
$currentWeek = date('W');
echo 'Semester-2, Week-', $currentWeek - $week1 + 1; // +1 because the count starts at 1.
break;
default:
echo $semesters[date('M')];
break;
}
Note that this can be refactored into smaller, more semantic parts.
I'm currenty looking into this tutorial to create a calendar. The only problem I have atm is that my months are in english instead of dutch. How can I change the output of 'july' to 'juli' ?
<?php
$vandaag = date("d"); // Current day
$maand = date("m"); // Current month
$jaar = date("Y"); // Current year
$dagen = cal_days_in_month(CAL_GREGORIAN,$maand,$jaar); // Days in current month
$vorigemaand = date("t", mktime(0,0,0,$maand-1,1,$jaar)); // Days in previous month
$begin = date("N", mktime(0,0,0,$maand,1,$jaar)); // Starting day of current month
$einde = date("N", mktime(0,0,0,$maand,$dagen,$jaar)); // Finishing day of current month
$vorigestart = $begin - 1; // Days of previous month in calander
$counter = 1;
$volgendeMaandCounter = 1;
if($begin > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $begin) < 0){
$date = (($vorigemaand - $vorigestart) + $counter);
$class = 'class="blur"';
}else if(($counter - $begin) >= $dagen){
$date = ($volgendeMaandCounter);
$volgendeMaandCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $begin + 1);
if($vandaag == $counter - $begin + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><a class="date">'. $date . '</a></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
?>
Thanks in advance!
try with this code:
setlocale(LC_TIME, 'de_DE', 'deu_deu');
/* print test date string */
echo strftime("%A, %d. %B %Y");
I am working on a calendar in PHP. With the calendar, I am able to query a mysql database and it will show the number of events for each day. The problem I'm having is that it is showing the start and end dates but i need it to also show all the days inbetween.Thus, showing that the days inbetween are also occupied. My current Table has DepDate (departure) AND RetDate (Return Date). Im thinking this has to be done through the mysql query. Any help would be awesome! Thanks buds
<script>
function goLastMonth(month, year, keyname){
if(month == 1) {
--year;
month = 13;
}
--month
var monthstring= ""+month+"";
var monthlength = monthstring.length;
var keyname = "<?php echo $searchTerm; ?>";
if(monthlength <=1){
monthstring = "0" + monthstring;
}
document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year+"&keyname="+keyname;
}
function goNextMonth(month, year, keyname){
if(month == 12) {
++year;
month = 0;
}
++month
var monthstring= ""+month+"";
var monthlength = monthstring.length;
var keyname = "<?php echo $searchTerm; ?>";
if(monthlength <=1){
monthstring = "0" + monthstring;
}
document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year+"&keyname="+keyname;
}
</script>
</head>
<?php
if (isset($_GET['day'])){
$day = $_GET['day'];
} else {
$day = date("j");
}
if(isset($_GET['month'])){
$month = $_GET['month'];
} else {
$month = date("n");
}
if(isset($_GET['year'])){
$year = $_GET['year'];
}else{
$year = date("Y");
}
$currentTimeStamp = strtotime( "$year-$month-01");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$currentTimeStamp = strtotime( "$day-$month-$year"); // added this to reset this value for prev* and next* code
$counter = 0;
?>
<?php
include ('plane.css');
/* ------------------------------------------------------------Calendar Creation---------------------------------------------------------------------- */
include ('planeh.html');
?>
<?php
//get previous month
$prevMonth = $month - 1;
$prevYear = $year;
if($prevMonth <= 0){
$prevMonth = 12;
$prevYear--;
}
$pMonthStr = "" + $prevMonth;
if(strlen($pMonthStr) <= 1)
$pMonthStr = "0" + $pMonthStr;
//get num of day for previous month
$previousTimeStamp = strtotime( "01-$pMonthStr-$prevYear");
$prevNumDays = date("t", $previousTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;
for($i = 1; $i < $numDays+1; $i++, $counter++){
$timeStamp = strtotime("$year-$month-$i");
if($i == 1) {
$firstDay = date("w", $timeStamp);
for($j = 0; $j < $firstDay; $j++, $counter++) {
$prevDay = $prevNumDays-$firstDay+$j+1;
echo "<td class = td3><div id = prev>$prevDay</div></td>";
}
}
if($counter % 7 == 0) {
echo"</tr><big></big><tr>";
}
/* Up to this point, everything is the development of the calendar */
$monthstring = $month;
$monthlength = strlen($monthstring);
$daystring = $i;
$daylength = strlen($i);
if($daylength <=0){
$daystring = "0".$daystring;
}
// links and date located on calendar
$todaysDate = date("m/d/y");
/* Indicates date located on calendar */
echo "<td align = center class = td3><div class = button>".$i."</a></div>";
$sqlEvent2 = mysql_query("select * FROM trips where DepDate = '".$year."-".$month."-".$i."'");
$num_rows = mysql_num_rows($sqlEvent2);
if(mysql_errno()){
echo "MySQL error ".mysql_errno().": "
.mysql_error()."\n<br>When executing <br>\n$query\n<br>";
}
echo '<div id="button">';
echo "<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$i."&year=".$year."&v=true' >".$num_rows."</a></td>";
echo '</div>';
}
echo "<tr>";
echo"</table>";
?>
<div id ="menu">
<?php include ('menu2.php');?>
</ul>
</div>
</div>
</tr>
</body>
</html>
All you need here is to reference the MySQL date and time functions, specifically datediff: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
An example would be:
SELECT start_dt, end_dt, datediff(end_dt, start_dt) as days_elapsed
FROM my_table
where start_dt and end_dt are both date valued.
"Thank you I have already solved my problem I think this one work"
I have a code that display only mondays of the month.
The next button is not calculating well.
Supposed to be it well end on 12(Month).
But it continuously adding the month.
This is my PHP code.
<?php
$current_month = date("n");
$month = ($_GET['m']) ? $_GET['m'] : date("n");
$previous_month = ($month - 1);
$next_month = ($month + 1);
$year = ($_GET['y']) ? $_GET['y'] : date("Y");
$previous_year = $year;
if($month == 0)
{
$month = 12;
$year--;
}
if($month == 13)
{
$month = 1;
$year++;
}
if($previous_month == 0)
{
$previous_month = 12;
$previous_year--;
}
$startDate = $year."-".$month."-01";
$endDate = $year."-".$month."-31";
$endDate = strtotime($endDate);
echo("<form name = 'formCalendar' id = 'formCalendar' action = 'calendar1.php?' method = 'get'>");
echo '<table border=1>';
echo '<tr>';
for($i = strtotime('Monday', strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))
echo '<td>'.date('d-M-y', $i).'</td>';
echo '</tr>';
echo(" </select>");
echo(" <input type = 'button' name = 'prev' value = '<<' onclick = 'location=\"calendar1.php?m={$previous_month}&y={$previous_year}\"'/>");
echo(" <input type = 'button' name = 'next' value = '>>' onclick = 'location=\"calendar1.php?m=". ($month + 1)."&y={$year}\"'/>");
echo(" </table>");
echo("<form>");
?>
Pls help fix the problem.
here is your complete working code
<?php
$current_month = date("n");
$month = (isset($_GET['m'])) ? $_GET['m'] : date("n");
$year = (isset($_GET['y'])) ? $_GET['y'] : date("Y");
$previous_month = ($month - 1);
$next_month = ($month + 1);
$previous_year = $year;
$next_year = $year;
if($previous_month==0)
{
$previous_month = 12;
$previous_year = $year-1;
}
if($next_month>12)
{
$next_month = 1;
$next_year = $year+1;
}
$startDate = $year."-".$month."-01";
$endDate = $year."-".$month."-31";
$endDate = strtotime($endDate);
echo("<form name = 'formCalendar' id = 'formCalendar' action = 'calender1.php?' method = 'get'>");
echo '<table border=1>';
echo '<tr>';
for($i = strtotime('Monday', strtotime($startDate)); $i <= $endDate; $i = strtotime('+1 week', $i))
echo '<td>'.date('d-M-y', $i).'</td>';
echo '</tr>';
echo(" </select>");
echo(" <input type = 'button' name = 'prev' value = '<<' onclick = 'location=\"calender1.php?m={$previous_month}&y={$previous_year}\"'/>");
echo(" <input type = 'button' name = 'next' value = '>>' onclick = 'location=\"calender1.php?m={$next_month}&y={$next_year}\"'/>");
echo(" </table>");
echo("<form>");
?>
Try this:
if ($month == 12) {
$previous_month = $month;
$next_month = 1;
} else {
$previous_month = ($month - 1);
$next_month = ($month + 1);
}
That solves the month issue
This question maybe little old post but I hope it helps to finetune the answer especially on the month issue when cross over the year
$previous_month = date("m", strtotime("-1 month", $year."-".$month."-01"));
$next_month = date("m", strtotime("+1 month", $year."-".$month."-01"));
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);