PHP Calendar table -
hi im trying make calendar table displays months in year , days in week.
i able make 1 month(nov 2016). want loop through out year , coming years.
can me?
<?php /* set default timezone */ date_default_timezone_set("asia/hong_kong"); /* set date */ $date = strtotime(date("y-m-d")); $day = date('d', $date); $month = date('m', $date); $year = date('y', $date); // $nextyear = strtotime('+1 month', $date); $firstday = mktime(0,0,0,$month, 1, $year); $title = strftime('%b', $firstday); $dayofweek = date('d', $firstday); $daysinmonth = cal_days_in_month(0, $month, $year); /* name of week days */ $timestamp = strtotime('next sunday'); $weekdays = array(); ($i = 0; $i < 32; $i++) { $weekdays[] = strftime('%a', $timestamp); $timestamp = strtotime('+1 day', $timestamp); } $blank = date('w', strtotime("{$year}-{$month}-01")); ?> <table class='table table-bordered' style="table-layout: fixed;"> <tr> <th colspan="32" class="text-center"> <?php echo $title ?> <?php echo $year ?> </th> </tr> <tr> <?php foreach($weekdays $key => $weekday) : ?> <td class="text-center"><?php echo $weekday ?></td> <?php endforeach ?> </tr> <tr> <?php for($i = 0; $i < $blank; $i++): ?> <td></td> <?php endfor; ?> <?php for($i = 1; $i <= $daysinmonth; $i++): ?> <?php if($day == $i): ?> <td><strong><?php echo $i ?></strong></td> <?php else: ?> <td><?php echo $i ?></td> <?php endif; ?> <?php if(($i + $blank) % 32 == 0): ?> </tr><tr> <?php endif; ?> <?php endfor; ?> <?php for($i = 0; ($i + $blank + $daysinmonth) % 32 != 0; $i++): ?> <td></td> <?php endfor; ?> </tr> </table>
as said in comments above can use 2 loops nested inside each other, 1 years, 1 months. way can execute given code based on different dates current one.
have @ approach. looking for, need fine tuning layout:
<?php define('number_of_columns', 37); function rendercalendermonth($date) { $day = date('d', $date); $month = date('m', $date); $year = date('y', $date); $firstday = mktime(0,0,0,$month, 1, $year); $title = strftime('%b', $firstday); $dayofweek = date('d', $firstday); $daysinmonth = cal_days_in_month(0, $month, $year); /* name of week days */ $timestamp = strtotime('next sunday'); $weekdays = array(); ($i = 0; $i < number_of_columns; $i++) { $weekdays[] = strftime('%a', $timestamp); $timestamp = strtotime('+1 day', $timestamp); } $blank = date('w', strtotime("{$year}-{$month}-01")); ?> <table class='table table-bordered' style="table-layout: fixed;"> <tr> <th colspan="<?php echo number_of_columns?>" class="text-center"> <?php echo $title ?> <?php echo $year ?> </th> </tr> <tr> <?php foreach($weekdays $key => $weekday) : ?> <td class="text-center"><?php echo $weekday ?></td> <?php endforeach ?> </tr> <tr> <?php for($i = 0; $i < $blank; $i++): ?> <td></td> <?php endfor; ?> <?php for($i = 1; $i <= $daysinmonth; $i++): ?> <?php if($day == $i): ?> <td><strong><?php echo $i ?></strong></td> <?php else: ?> <td><?php echo $i ?></td> <?php endif; ?> <?php if(($i + $blank) % number_of_columns == 0): ?> </tr><tr> <?php endif; ?> <?php endfor; ?> <?php for($i = 0; ($i + $blank + $daysinmonth) % number_of_columns != 0; $i++): ?> <td></td> <?php endfor; ?> </tr> </table> <?php } // =========================== /* set default timezone */ date_default_timezone_set("asia/hong_kong"); ($iterateyear=2016; $iterateyear<2018; $iterateyear++) { ($iteratemonth=1; $iteratemonth<=12; $iteratemonth++) { /* set date */ $date = strtotime(sprintf('%s-%s-01', $iterateyear, $iteratemonth)); rendercalendermonth($date); } }
Comments
Post a Comment