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.
Related
I need to include a php calendar on the sidebar of my page.
I am using a snippet I found weeks ago, as I have used it before and it works fine. But this time, I have to add next and previous buttons to display the previous or next month...
My question is... do I need to modify the php that generates the current month?
this is what I have so far:
<table class="month">
<tr class="days">
<td>Mon</td>
<td>Tues</td>
<td>Wed</td>
<td>Thurs</td>
<td>Fri</td>
<td>Sat</td>
<td>Sun</td>
</tr>
<?php
$today = date("d"); // Current day
$month = date("m"); // Current month
$year = date("Y"); // Current year
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month
$lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month
$start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month
$finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of current month
$laststart = $start - 1; // Days of previous month in calander
$counter = 1;
$nextMonthCounter = 1;
if($start > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $start) < 0){
$date = (($lastmonth - $laststart) + $counter);
$class = 'class="blur"';
}else if(($counter - $start) >= $days){
$date = ($nextMonthCounter);
$nextMonthCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $start + 1);
if($today == $counter - $start + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><span class="dayWrap">'. $date . '</span></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
?>
</table>
<div class="changeMonthLinks">
<a class="col-xs-12" href="">< Prev</a>
<a class="col-xs-12 aright" href="">Next ></a>
</div>
I just don't know how to proceed...or what do I need to add in the anchor tags :S
Any help will be appreciate it.
Thank you
Thank you!!
I've added $now for the param ?now in the url and I'm parsing it with strtotime to the variable $dtNow, all the date functions are extended and the links in the bottom are extended with ?now=$dtNow + 1 month and ?now=$dtNow - month
Here's the code
<?php
$now = '';
if(isset($_GET['now']))
$now = $_GET['now'];
$dtNow = strtotime($now);
if(!$dtNow)
{
$dtNow = time();
}
echo "<h1>Today is " . date('Y-m-d', $dtNow) . "</h1>";
?>
<table class="month">
<tr class="days">
<td>Mon</td>
<td>Tues</td>
<td>Wed</td>
<td>Thurs</td>
<td>Fri</td>
<td>Sat</td>
<td>Sun</td>
</tr>
<?php
$today = date("d", $dtNow); // Current day
$month = date("m", $dtNow); // Current month
$year = date("Y", $dtNow); // Current year
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month
$lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month
$start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month
$finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of current month
$laststart = $start - 1; // Days of previous month in calander
$counter = 1;
$nextMonthCounter = 1;
if($start > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $start) < 0){
$date = (($lastmonth - $laststart) + $counter);
$class = 'class="blur"';
}else if(($counter - $start) >= $days){
$date = ($nextMonthCounter);
$nextMonthCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $start + 1);
if($today == $counter - $start + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><span class="dayWrap">'. $date . '</span></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
?>
</table>
<div class="changeMonthLinks">
<a class="col-xs-12" href="?now=<?php echo date('Y-m-d', $dtNow - 30*24*60*60); ?>">< Prev</a>
<a class="col-xs-12 aright" href="?now=<?php echo date('Y-m-d', $dtNow + 30*24*60*60); ?>">Next ></a>
</div>
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");
"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 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
I have a calendar application, and in that calendar application, there is a "mini view" of a calendar. That little widget only displays the days of the currently chosen month and when you click the number it opens a new page and sends GET data to that new page (to display MySQL info, etc.)
The point is: this little mini-calendar doesn't do much at all, and I'm working to turn it into a partial in Zend Framework MVC. We have jQuery as well. I'm wondering if there is any built-in code that will easily do what we are trying to do with our own code.
Our code (done procedurally):
<?php
/***
This script file is the left panel calendar (small)
*/
//Required variables initializion starts (important in the case of create new event, to avoid PHP notices).
$day = "";
$month = "";
$year = "";
$sel = "";
$what = "";
$page = "index.php";
$param = "";
$index = "";
$functionLast = "goLastMonth";
$functionNext = "goNextMonth";
$sendFunction = "sendToForm";
if(isset($_GET['index'])) //if index page
{
$index = $_GET['index'];
}
if(isset($_GET['type'])) //if sype is set
{
$param = "&type=".$_GET['type'];
}
if(isset($_GET['page'])) //if page is set
{
$page = "calendar.php";
$param = '&page=calendar';
$functionLast = "getLastMonth";
$functionNext = "getNextMonth";
$sendFunction = "sendToTextBox";
}
if(!isset($calWidth) && !isset($calHeight)) //cal width /height check
{
$calWidth = CALENDAR_WIDTH;
$calHeight = CALENDAR_HEIGHT;
}
if(isset($_GET["day"])) //if day is set
$day = $_GET["day"]; //get it
if(isset($_GET["month"])) //if month is set
$month = $_GET["month"]; //..
if(isset($_GET["year"])) //..
$year = $_GET["year"]; //
if(isset($_GET["sel"]))
$sel = $_GET["sel"];
if(isset($_GET["what"]))
$what = $_GET["what"];
if(isset($_GET['date']))
{
$date = $_GET['date'];
list($year,$month,$day) = explode("-",$date); //split date into pieces
}
if($day == "") $day = date("j"); //if day is blank, get today
if($month == "") $month = date("m"); //if month is blank, get this month
if($year == "") $year = date("Y"); //if year is blank, get this year
//echo $day."-".$month."-".$year;die;
//echo '<br>';
if(!checkdate($month, $day, $year)) { //if not a valida date
if(isset($_GET["month"])) { //try to get number of days for this month as this seems the last day of the month. for example if today is 31 of August and you are calling ?month=9&year=2009 it gives you wrong results
$day = date("t", strtotime($year . "-" . $month . "-01")); //so give you 30.
}
}
$printabledate = $year."-".$month."-".$day;
$currentTimeStamp = strtotime("$year-$month-$day");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;
?>
<br />
<div id="loading1" class="a_loading1">
<iframe src="<?php echo SITE_URL?>/loading-msg.php" scrolling="no" frameborder="0" class="markup a_position"></iframe>
</div>
<table class="mini-cal-table">
<tr class="tprowbgcolor">
<td class="arrow" colspan='1' align="center"><input type='button' class='buttonleft' onclick='<?php echo "$functionLast($month,$year,\"$page\",\"$index\")"; ?>' onmousedown="this.className='maincalbutton_active_left'" onmouseout="this.className='buttonleft'" /></td>
<td class="title" colspan='5'><span class='title'><?php echo $monthName . " " . $year; ?></span></td>
<td class="arrow" colspan='1' align="center"><input type='button' class='buttonright' onclick='<?php echo "$functionNext($month,$year,\"$page\",\"$index\")"; ?>' onmousedown="this.className='maincalbutton_active_right'" onmouseout="this.className='buttonright'" /></td>
</tr>
<tr>
<td class='wd-titles'>Su</td>
<td class='wd-titles'>Mo</td>
<td class='wd-titles'>Tu</td>
<td class='wd-titles'>We</td>
<td class='wd-titles'>Th</td>
<td class='wd-titles'>Fr</td>
<td class='wd-titles'>Sa</td>
</tr>
<tr>
<?php
for($i = 1; $i < $numDays+1; $i++, $counter++)
{
$timeStamp = strtotime("$year-$month-$i");
if($i == 1)
{
// Workout when the first day of the month is
$firstDay = date("w", $timeStamp);
for($j = 0; $j < $firstDay; $j++, $counter++)
echo "<td> </td>";
}
if($counter % 7 == 0) {
echo "</tr><tr>";
}
if(date("w", $timeStamp) == 0) {
//$class = "class='weekend'";
$tdclass = "weekend";
} else {
if($i == date("d") && $month == date("m") && $year == date("Y")) {
//$class = "class='today'";
$tdclass = "today";
}
else {
//$class = "class='normal'";
$tdclass = "normal";
}
}
$zero = "";
if($i < 10 )
{
$zero = "0";
}
$month = round($month);
if($month < 10)
{
$month = "0".$month;
}
$date = $year."-".$month."-".$zero.$i;
?>
<td class="<?php echo $tdclass?>"><?php
if(!isset($_GET['page'])) {
?><a href='<?php echo SITE_URL; ?>/agenda.php?date=<?php echo $year; ?>-<?php echo $month; ?>-<?php echo $zero.$i; ?>'><?php echo $i?></a>
<?php } else {
?>
<a onclick='<?php echo "$sendFunction($i,\"$date\",$numDays,\"$index\",\"$type\")"; ?>'><?php echo $i?></a>
<?php
}
?></td>
<?php
}
?>
</tr>
</table>
<script language="javascript" type="text/javascript">
//<![CDATA[
function goLastMonth(month,year,page,index) {
// If the month is January, decrement the year.
if(month == 1) {
--year;
month = 13;
}
var url =
document.location.href = page+"?month="+(month-1)+"&year="+year+"<?php echo $param?>";
}
function goNextMonth(month,year,page,index)
{
// If the month is December, increment the year.
if(month == 12)
{
++year;
month = 0;
}
document.location.href = page+"?month="+(month+1)+"&year="+year+"<?php echo $param?>";
}
//]]>
</script>
jQuery has many good calendar options, the following being a part of the UI core:
http://jqueryui.com/demos/datepicker/
Also Zendx supports the jqueryui datepicker. Example