A PHP Class
It's a PHP class that will output the dates of the given month in array. But in order to make the dates fit their place in the week, the class outputs zeros in the place of "empty" days. So the first week of a month starting in Wednesday would be this array:[0, 0, 1, 2, 3, 4, 5]
This allows you to loop through the returned arrays without worrying where to start the week. If you use a table, you could simply output " " when the date is 0 and output the number (and colored background) when the date is a number. Here is an example in HTML:
Using the output in HTML:
<table>
<tr><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th><th>Sun</th></tr>
<?php for($i=1;$i<=$num_weeks;$i++):?>
<tr>
<?php $days=$_calendar->days($m[0], $m[1], $i, $num_weeks);
foreach($days as $day):?>
<td><?=$day?$day:" "?></td>
<?php endforeach;?>
</tr>
<?php endfor;?>
</table>
Easy, eh? We just output a table row for every week in the month ($num_weeks is also a method in the class as you'll see below) and for the days with date we output the date (otherwise just blank space).
You can see how it works live in this period calendar.
The PHP Code
Now, here is how it all works. I created a class to hold 3 functions:num_weeks($month, $year) - to calculate the number of weeks in the given month and year
first_day($month, $year) - to find out which day of the week is the first day of a given month.year
days($month, $year, $week, $num_weeks=0) - this is the main method that returns the array of 7 days. The week starts in Monday.
Here is the full code below and some comments to it:
function num_weeks($month, $year)
{
// every month has at least 4 weeks
$num_weeks=4;
// finding where it starts
$first_day = $this->first_day($month, $year);
// if the first week doesn't start on monday
// we are sure that the month has at minimum 5 weeks
if($first_day!=1) $num_weeks++;
// find the "widow" days (i.e. empty cells in the 1st week)
$widows=$first_day-1;
$fw_days=7-$widows;
if($fw_days==7) $fw_days=0;
// number of days in the month
$numdays=date("t",mktime(2, 0, 0, $month, 1, $year));
if( ($numdays - $fw_days) > 28 ) $num_weeks++;
// that's it!
return $num_weeks;
}
Then the first_day function:
function first_day($month, $year)
{
$first_day= date("w", mktime(2, 0, 0, $month, 1, $year));
if($first_day==0) $first_day=7; # convert Sunday
return $first_day;
}
And here is the most important one:
// here $week is the week number when we go in a loop
// (see the html code that I posted earlier to get better idea)
function days($month, $year, $week, $num_weeks=0)
{
$days=array();
// this is just to avoid calling num_weeks every time
// when you loop through the weeks
if($num_weeks==0) $num_weeks=$this->num_weeks($month, $year);
// find which day of the week is 1st of the given month
$first_day = $this->first_day($month, $year);
// find widow days (first week)
$widows=$first_day-1;
// first week days
$fw_days=7-$widows;
// if $week==1 don't do further calculations
if($week==1)
{
for($i=0;$i<$widows;$i++) $days[]=0;
for($i=1;$i<=$fw_days;$i++) $days[]=$i;
return $days;
}
// any other week
if($week!=$num_weeks)
{
$first=$fw_days+(($week-2)*7);
for($i=$first+1;$i<=$first+7;$i++) $days[]=$i;
return $days;
}
# only last week calculations below
// number of days in the month
$numdays=date("t",mktime(2, 0, 0, $month, 1, $year));
// find orphan days (last week)
$orphans=$numdays-$fw_days-(($num_weeks-2)*7);
$empty=7-$orphans;
for($i=($numdays-$orphans)+1;$i<=$numdays;$i++) $days[]=$i;
for($i=0;$i<$empty;$i++) $days[]=0;
return $days;
}
That's it! Any questions?
PHP is the best server side scripting language! I was is search of this php calender code ,thanks for sharing. I hope to see some more interesting php codes in this blog in near future.
ReplyDeleteEcommerce Design
I was looking for the same calendar info for a long time thanks for sharing.
ReplyDeleteIt is great for beginner PHP learners.Thanks for all these information i hope to see more interesting and knowledgeable blog in near future.
ReplyDeleteFind a php calendar here: http://bohemiawebsites.com/component/option,com_jdownloads/Itemid,382/cid,68/task,view.download/
ReplyDeleteYou can find more than 300 pages worth of php tutorials at http://bohemiawebsites.com/Our-Blog.html
PHP contain the many class.but calender class provide the long time information.it also show the day and hours.actually its is great.
ReplyDeleteGreat Post for PHP learners..This is wonderful post. I do hope you intend to post more of these types of information. Thank you for this interesting information...
ReplyDeleteWeb Designing Company in Chandigarh
EMC SAN Online Training is a best course all top company requires this,W3 D technology provides EMC SAN Online Training course with Certification and E-books , Videos
DeleteEMC SAN Online Training | Storage Area Network Course
Thanks. It's awesome ,it is realy helpful
ReplyDeleteWeb Development Company in Indore
I don't know how to use it to print calendar in HTML
ReplyDeleteAfter make index.php file with HTML code from above, and then file calndar.class.php and put to the index.php code only calendar header with days show up... where are days?
ReplyDeleteVery nice explanation on creating a calender.
ReplyDeletePHP Training in chennai
Thank you for the informative post. Must appreciated.
ReplyDeleteShashaa
HTML5 Course in Velachery
Thanks for sharing this wonderful information.Its very nice blog PHP
ReplyDeletethanks for sharing
ReplyDeleteSAP GTS Training In Hyderabad
thanks for giving this type of informative blog,we learn more things about tips for seo which is involves the detailed information about optimization of blogger.
ReplyDeleteSAP Training in Chennai
EMC SAN Online Training is a best course all top company requires this,W3 D technology provides EMC SAN Online Training course with Certification and E-books , Videos
ReplyDeleteEMC SAN Online Training | Storage Area Network Course
Nice blog on php development. The information you are providing through this blog is very helpful to beginner Php Developers .
ReplyDeleteThanks
You have written wonderful article. Greetings and thank you...
ReplyDelete6 weeks php Industrial Training
PHP 6 weeks Industrial Training
6 weeks php industrial training in chandigarh
6 month php training in chandigarh
Have you been thinking about the power sources and the tiles whom use blocks I wanted to thank you for this great read!! I definitely enjoyed every little bit of it and I have you bookmarked to check out the new stuff you post. java training jalandhar
ReplyDeleteThis is best PHP tutorial in youtube. you can learn full coding and also the best expamles in here. This the best of learning and earning ways of youtube.
You can the full boston php tutorial.
click here PHP tutorial
for more youtube tutorials
website php tutorial for beginners
Nice article
ReplyDeleteThanks for sharing the informative blog.
ASP.NET Training In Bangalore
Nice to read your article! very informative.So, please keep posting PHP Stuff here Thanks.......
ReplyDeleteThis blog explains the details of PHP. This helps to learn about what are all the different method is there. And the working methods all of that are explained here. Informative blog.For more Details Or information Visit Intellipaat
ReplyDeleteHi ,
ReplyDeleteNice article.Thank you for sharing the information.I really glad enjoy read that topic.I appreciate post for your.
php Training in Chandigarh
Great post..thanks for sharing the valuable content..good work keep it up.
ReplyDeleteBest software Training institute in Bangalore
fantastic information! keep sharing.
ReplyDeleteRead all the information that i've given in above article. It'll give u the whole idea about it.
ReplyDeleteDevOps Training in Bangalore
DevOps Training in Pune
DevOps Online Training
Great blog!! Worthy information that you shared is very useful for my studies. happy sharing!!
ReplyDeleteDevops Training in Chennai
Devops Certification in Chennai
Big Data Training in Chennai
German Classes in Chennai
German Language Classes in Chennai
Python Training in Chennai
Devops Training in Porur
Maka dari itu, dikesempatan ini admin akan memperkenalkan kepada anda beberapa Situs QQ Terbaru dan Terpercaya yang ada di Indonesia pada tahun 2019 – 2020 ini. Diantara para pembaca artikel ini, tentu saja mencari keberuntungan dengan bermain pada website baru.
ReplyDeleteasikqq
dewaqq
sumoqq
interqq
pionpoker
bandar ceme terbaik
hobiqq
paito warna terlengkap
syair sgp
Nice...
ReplyDeletebitwise aptitude questions
how to hack flipkart legally
zenq interview questions
count ways to n'th stair(order does not matter)
zeus learning subjective test
ajax success redirect to another page with data
l&t type 2 coordination chart
html rollover image
hack android phone using cmd
how to hack internet speed upto 100mbps
Good information
ReplyDeleteBest QA / QC Course in India, Hyderabad. sanjaryacademy is a well-known institute. We have offer professional Engineering Course like Piping Design Course, QA / QC Course,document Controller course,pressure Vessel Design Course, Welding Inspector Course, Quality Management Course, #Safety officer course.
QA / QC Course
QA / QC Course in india
QA / QC Course in hyderabad
ReplyDeleteNice post...
3d-modeler-resume-samples
3d modeler resume samples
accounting-assistant-resume-sample
accounting-clerk-resume-sample
accounting-manager-resume-samples
account-manager-resume-examples
accounts-payable-resume-sample
admin-manager-resume-samples
advocate-resume-sample
advocate-resume-sample
Thanks for sharing such a wonderful content.
ReplyDeleteWe are the best waterproofing services in Hyderabad.We are providing all kinds of leakage services which includes bathroom,roof,wash area,water tank,wall cracks,kitchen leakage services in Hyderabad. With trust and honest, we solve the issue as quick as possible.We serve you better compared to others.
Best waterproofing services in hyderabad
bathroom leakage services in hyderabad
roof leakage services in hyderabad
water tank leakage services in hyderabad
kitchen leakage services in hyderabad
Hyderabad waterproofing services
Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about php
ReplyDeleteweb designing training in chennai
web designing training in omr
digital marketing training in chennai
digital marketing training in omr
rpa training in chennai
rpa training in omr
tally training in chennai
tally training in omr
It's an excellent piece of information. keep updating me, thanks for sharing this blog.
ReplyDeleteangular js training in chennai
angular js training in tambaram
full stack training in chennai
full stack training in tambaram
php training in chennai
php training in tambaram
photoshop training in chennai
photoshop training in tambaram
Thanks for sharing this wonderful information.Its very nice blog PHP
ReplyDeletejava training in chennai
java training in annanagar
aws training in chennai
aws training in annanagar
python training in chennai
python training in annanagar
selenium training in chennai
selenium training in annanagar
It's awesome.
ReplyDeletehttps://whitelabelfox.com/gojek-clone-app/
Thanks for Sharing a Very Informative Post & I read Your Article & I must say that is very helpful post for us.
ReplyDeleteAWS Training in Pune
Thanks for choosing this specific Topic. As i am also one of big lover of this. Your explanation in this context is amazing. Keep posted these overwarming facts in front of the world.
ReplyDeletePrinter customer support number
Very Informative blog thank you for sharing. Keep sharing.
ReplyDeleteBest software training institute in Chennai. Make your career development the best by learning software courses.
best rpa training in chennai
best msbi training institute in chennai
cloud computing courses in chennai
I believe there are many more pleasurable opportunities ahead for
ReplyDeleteindividuals that looked at your site.
mysql dba online training in Chennai
Unix classes in Chennai
Best IT training institute in Chennai
This PHP calendar class tutorial is an impressive resource! For anyone managing online events or looking to integrate time-bound activities, having a flexible calendar feature can make a big difference. In digital marketing courses, like the Advanced Digital Marketing Course By Digiperform, understanding tools that can enhance user experience is crucial. Great calendar functionality can boost engagement, especially for sites that rely on event scheduling. Thanks for the detailed breakdown!
ReplyDelete