Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBjörn Norén
9,569 PointsNeed help with calendar please! PHP
Hello there! I want to learn how to create a working calendar so I find a tutorial on youtube & I created this code but it's not working properly. Can someone show where it's failing so I can get it to work?
Thanks!
´´´PHP <?php
date_default_timezone_set('Asia/Tokyo');
// Get prev & next month if (isset($_GET['ym'])){ $ym = $_GET['ym']; } else { $ym = date('Y-m'); }
$timestamp = strtotime($ym,"-01"); if($timestamp === false){ $timestamp = time(); }
// Today $today = date('Y-m-d', time());
// For H3 title $html_title = date('Y / m', $timestamp);
// Create prev & next month link $prev = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)-1, 1, date('Y', $timestamp))); $next = date('Y-m', mktime(0, 0, 0, date('m', $timestamp)+1, 1, date('Y', $timestamp)));
// Number of days in the month $day_count = date('t', $timestamp);
// 0:Sun 1:Mon 2:Tue ... $str = date('w', mktime(0, 0, 0, date('m', $timestamp), 1, date('Y', $timestamp)));
// Create Calendar!! $weeks = array(); $week = '';
// Add empty cell $week .= str_repeat('<td></td>', $str);
for ( $day = 1; $day<=$day_count; $day++, $str++){
$date = $ym. '-'.$day;
if ($today==$date){
$week .= '<td class="today">' .$day;
} else {
$week .= 'td'.$day;
}
$week .= '</td>';
// End of the week OR End of the month
if ($str % 7==6 || $day == $day_count){
if($day==$day_count){
// Add empty cell
$week .= str_repeat('<td></td>', 6 - ($str % 7));
}
$weeks[] = '<tr>' .$week. '</tr>';
// Prepare for new week
$week='';
}
}
?>
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
.container{
margin-top: 80px;
}
th {
height: 30px;
text-align: center;
}
tr {
height: 100px;
}
.today {
background-color: orange;
}
th:nth-of-type(7),td:nth-of-type(7){
color: blue;
}
th:nth-of-type(1),td:nth-of-type(1){
color: red;
}
</style>
</head>
<body>
<div class="container">
<h3><a href="?ym=<?php echo $prev; ?>"><</a><?php echo $html_title; ?><a href="?ym=<?php echo $next; ?>">></a></h3>
<br />
<table class="table table-bordered">
<tr>
<th>S</th>
<th>M</th>
<th>T</th>
<th>W</th>
<th>T</th>
<th>F</th>
<th>S</th>
</tr>
<?php
foreach ($weeks as $week){
echo $week;
}
?>
</table>
</div>
</body>
</html>
´´´
1 Answer
Jacob Mishkin
23,118 PointsI would suggest you review this tutorial [here[(https://www.startutorial.com/articles/view/how-to-build-a-web-calendar-in-php) This will help you set up a calendar by using Classes. First off, I will say that creating a calendar is extremely difficult to do, there are a lot of things you need to account for. That being said, checkout the link, and take a look.