Jump to content

Getting The Day Of The Month


jimfog

Recommended Posts

I want to build a calendar and in which, the days of the month are depicted, I know that the getDate method does exactly that. But, we are talking here about multiple table cells and with the content(days of the month) having to take into consideration that NOT all of the months have the same number of days. 2 Questions:

  1. Can the above be done solely with javascript, Need to note that the calendar will store events, so PHP will come into the play inevitable.
  2. Is it maybe better that until day 28(the min number of days a month can have), i must enter just the numbers in the cells and THEN use some method to enter the remaining ones, when the month dictates that-when a specific month has more than 28 days.

Link to comment
Share on other sites

Javascript can do anything with data. You don't need anything else if you want to put together an empty calendar. Adding data and saving it will require PHP. You can have an array of months and the number of days they have: var daysInMonths = [31, 28, ... ]; Find out what day of the week the first day of that month is, plot empty cells until that day, then just make a loop that prints cells with numbers in them until the amount of days in the specified month.

Link to comment
Share on other sites

If it is not to much of a trouble can you write please some code example?

Link to comment
Share on other sites

Start basic and go from there. Start with something that just prints the month, from the first day to the last, in one row. Then change it so that it starts a new row every sunday or monday or whatever you want. Then change it to calculate how many days in the week are before the first of the month, and print those days before the first. The same for the end of the month, print extra days for each day in the week after the month end. Don't try to do the entire thing at once, do it one step at a time. You can use the Date object to figure out which day of the week it is and which day of the month, that's really all you need to know. You don't need to know how many days are in the month to start with, just keep going until you reach the end.

Link to comment
Share on other sites

Is sounds easy but for an inexperienced like me it is not. I will try though-step by step as you said.

Link to comment
Share on other sites

I found a useful tutorial here: http://jszen.blogspot.com/2007/03/how-to-build-simple-calendar-with.html. Another option is to use jQuery's datepicker widjet, the only issue with the second solution(jQuery datapicker) is if a actually need all this code when you download the widget. I am trying to think what is better, construct the calendar my own, "manually" or use a ready made solution from jQuery. I really do not know the benefits/drawbacks of each approach. What do you think?

Link to comment
Share on other sites

The benefits of jQuery is having it easier. The benefits of doing it with plain Javscript is learning more and not having all the extra jQuery library to load.

Link to comment
Share on other sites

Need to note that the calendar will store events, so PHP will come into the play inevitable.
The calendar does not need to store events, you just need to link calendar month or date to events stored in database at specific dates. What you could do is create an id ref for the date in the cell ie id="dateid_20110104" 4th jan 2011, then gather the event from database on that day and assign a class with identical ref for the class class="dateid_20110104", with this you can use jquery to highlight the date within the calender, and make it clickable to show the event (elements with class class="dateid_20110104" as there could be more than one), OR using the same principle, assign id ref "dateid_201101" for year and month, and show all events with matching class name.
Link to comment
Share on other sites

Before proceeding with your solution Dsonesuk i need to make the following question regarding jQuery's datepicker. I downloaded the datepicker and saw that the file contains a lot of code, i suppose it is there to support various options also. Am i correct? Much of the time and depending the needs all this code is useless, so "manual" coding is not a bad alternative.

Link to comment
Share on other sites

It depends, if it provides some sort of date reference so you link it to event dates from your DB, and also highlight the dates on the calendar itself, then most of the work is already done for you. I have used chronoforms datepicker, which involves the user clicking the datepicker icon next to input field, this shows a calender where you select dates, and these dates will appear in the input fields, you then can use values to search your DB events list, and then show these events by using several different methods, one being Ajax. I have not used it to highlight specific dates say from a events list on the calendar itself, I'm sure that could be achieved somehow. The most important required feature here is the providing of a reference from the calendar to tie in a db reference when date date is selected.

Link to comment
Share on other sites

The most important required feature here is the providing of a reference from the calendar to tie in a db reference when date date is selected.
Correct. I have not reached to the point yet of connecting the calendar with the db. I am in the process of making the markup first. I think Chronoforms is related to Joomla and my application is NOT based on it.
Link to comment
Share on other sites

Regarding the markup there is something that keeps my mind annoyed. Since the calendar is going to appear in many webpages of the site do you think i should enclose it in a php function and call it when appropriate. Currently i use the following code:

<div  class="calendar" >  </div>

And i use javascript to append in the above the calendar markup(which is made with javascript). How should i proceed with it you think?

Link to comment
Share on other sites

As i understand it! you place a script within a element to call the function that dynamically produces the calendar, then why include it in an php file and include it! As it is basically just producing exactly the same result. All you need to make sure is the script is placed in and external js file and linked to.

Link to comment
Share on other sites

Well... i think you are right. But let us focus a little. You said that i create the calendar dynamically-you mean using javascript? Correct? My question was based on the following: I am reading a php book, in which the scripts are comprised of functions which get called, the functions contain the html. That is why i asked if it was better to have php functions. BUT FROM WHAT I UNDERSTAND, I AM OK WITH THE CURRENT SCHEME ALREADY USED. Do you think that is better to construct the calendar with php and abandon javascript, or it does not matter. Sorry, but i am confused a little regarding when php comes in play and when js does.

Link to comment
Share on other sites

in my case,is the route I follow the proper one?:1. create the calendar with js2.append the calendar to a Div element in index.php3.I will use php only to store the events in the databasewhat do you think about the above logic

Link to comment
Share on other sites

I think that if you're storing the events in the database, then it makes sense to also output the calendar markup using PHP. The only time I use calendars generated using Javascript are when I'm asking the user to pick a date, where it pops up a little calendar where they can just click on one of the dates.

Link to comment
Share on other sites

I must admit as work is slow, i have produced a php ajax version that has slide left/right effect to different years/months, highlights current date and event dates, and when you click these events the information about these events slide down into view. I also started on a fully javascript version, using the link you provided until work picked up again. But as you said you where going to use the calendar to store/retrieve event data from database, I thought the php ajax version would be the best way to go, but you started using a javascript version instead.

Link to comment
Share on other sites

Ok, you said to make the calendar with php. But i want to get the date from the user's pc, sth done with js date object. PHPs date function gets the time from the server. Unless i send the js date info(i do not know why) to the php code, and php uses this info after all. Probably i will use ajax to send the user's time in the server but i do not have the slightest idea how to do that. Its a pity some much waste of time to build the calendar in js only to realize it won't serve my goals.

Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script type="text/javascript">/*<![CDATA[*//*---->*/var xmlhttpvar current_id_ref="";function ajax_update(str,id_ref){xmlhttp=GetXmlHttpObject();if (xmlhttp==null)  {  alert ("Your browser does not support AJAX!");  return;  }var url=id_ref+".php";url=url+"?"+str;url=url+"&sid="+Math.random();xmlhttp.onreadystatechange=stateChanged;xmlhttp.open("GET",url,true);xmlhttp.send(null);current_id_ref=id_ref;}function stateChanged(){if (xmlhttp.readyState==4){document.getElementById(current_id_ref).innerHTML=xmlhttp.responseText;}}function GetXmlHttpObject(){if (window.XMLHttpRequest)  {  // code for IE7+, Firefox, Chrome, Opera, Safari  return new XMLHttpRequest();  }if (window.ActiveXObject)  {  // code for IE6, IE5  return new ActiveXObject("Microsoft.XMLHTTP");  }return null;}window.onload=function(){var jsDate=new Date();ajax_update('jsDate='+jsDate, 'JS_to_PHP_Date')}/*--*//*]]>*/</script><style type="text/css"></style></head><body><div id="JS_to_PHP_Date"></div></body></html>

JS_to_PHP_Date.php

<?php$current_JS_Date=date("d/m/Y");if(isset($_GET['jsDate'])){$current_JS_Date = $_GET['jsDate'].' javascript returned date through PHP variable';}echo $current_JS_Date;?>

Link to comment
Share on other sites

Ok, thanks a lot for your help. I can understand to some extent the ajax code. Regarding the calendar though-does your code assume that i must use the same js-caledar i have already made? It is this point that is critical. Or i must remake the calendar in PHP? Explain it a little cause i am an amateur in ajax-with the above code is it only the date sent?

Link to comment
Share on other sites

when the page is loaded jsDate will retrieve todays date-time using javascripts new Date() window.onload=function(){var jsDate=new Date();ajax_update('jsDate='+jsDate, 'JS_to_PHP_Date')} The function ajax_update() is called and GET querystring 'jsDate=' along with the JavaScript variable jsDate, and a ref to the id value 'JS_to_PHP_Date' as in

<div id="JS_to_PHP_Date"></div>

where the returned value be placed ( I also use this ref as the php filename that will process this specific passed data. (JS_to_PHP_Date.php)), are passed along with the function.

function ajax_update(str,id_ref) //values sent to function example 'str = jsDate=Thu Dec 15 2011 15:03:59 GMT 0000 (GMT Standard Time)' , 'JS_to_PHP_Date'{xmlhttp=GetXmlHttpObject();if (xmlhttp==null)  {  alert ("Your browser does not support AJAX!");  return;  }var url=id_ref+".php"; // becomes JS_to_PHP_Date.php the page that will receive $_GET[] query to processurl=url+"?"+str; //becomes JS_to_PHP_Date.php?jsDate=Thu Dec 15 2011 15:03:59 GMT 0000 (GMT Standard Time)url=url+"&sid="+Math.random(); // and to prevent duplicate cached result being used with same querystring add ref to random number//send to defined page set in url with querystring valuesxmlhttp.onreadystatechange=stateChanged;xmlhttp.open("GET",url,true);xmlhttp.send(null);current_id_ref=id_ref;}

JS_to_PHP_Date.php

<?php$current_JS_Date=date("d/m/Y"); set php variable date using PHP (don't panic) if(isset($_GET['jsDate'])) //check if querystring 'jsDate'  has been sent{$current_JS_Date = $_GET['jsDate'].' javascript returned date through PHP variable'; // if yes! overwrite current php date value with javascript date passed with 'jsDate,}echo $current_JS_Date; // echo out result?>

Back to Ajax javascript code at the same time statechange is called function stateChanged(){if (xmlhttp.readyState==4)and if readyState = 4, querystring recieved and processed{document.getElementById(current_id_ref).innerHTML=xmlhttp.responseText; send the returned echo text to specfic id (JS_to_PHP_Date)}}

<div id="JS_to_PHP_Date">Thu Dec 15 2011 15:03:59 GMT 0000 (GMT Standard Time) javascript returned date through PHP variable</div>

The end

Link to comment
Share on other sites

Ok got that. But will i have to make a new php calendar? Yes or No.

Link to comment
Share on other sites

I really can't answer that, in theory you should be able to use your javascript calendar, after all I use javascript/jquery to send ajax querystring to update the calendar as i go months and years, This can be done solely by updating the function that dynamically reproduces the calendar, I don't know exactly how or what you truly attempting use the calendar for? show events?, book events? is this part of some form where the user clicks a date, and this date is stored? theres no reason you can't click a date, which will pass a date such as 20111215 as a ajax reference to search for events on the same day within a mysql database, and it then shows found results, its perfectly feasible this can all be done using your javascript calendar, you just, as i said before have to work out the best option to use as a reference (as in 20111215) passed on the onclick event, to tie in with a reference to bring all the required results in the database.

Link to comment
Share on other sites

To answer the most important question. The calendar will be used to book events. So if the user wants to book an event that it is on 25th of december this year. Then that specific date must be stored in the db. First, of course, he must click in the calendar on the 25th(on the corresponding box/number) to see that specific event in the screen. And then he will make the booking. I hope you got the picture now.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...