jack13580 Posted May 1, 2013 Share Posted May 1, 2013 (edited) I am making my own custom calendar application/script and have just finished with one of its functions This function is to calculate the days of the month before the current month according to what day the current month started on and display them accordingly The logic I used to get this to work which took me a week to figure this out works most of the time, my questions are: 1. Is there a better way of doing this? 2. Is there anything I may have overlooked? 3. What may be the cause for the incorrect numbers shown for example go back 2 months to see how off it is $numDays = date("t", $currentTimeStamp); $counter = 0; for($i = 1; $i < $numDays+1; $i++, $counter++){ $monthstring = $month; $monthlength = strlen($monthstring); $daystring = $i; $daylength = strlen($daystring); $timeStamp = strtotime("$year-$month-$i"); $timeStamp_before = strtotime("$year-$month_before-$i"); $month_before = ($month == 1) ? "12" : $month-1; $year_before = ($month == 1) ? $year-1 : $year; $firstDay = date("w", $timeStamp); $days_before = date("j", $timeStamp_before); $days_before_thisMonth = $days_before - $firstDay; $days_before_thisMonth = $days_before_thisMonth+1; for($if1=0;$if1!=1 && $i < 2;$if1++){ $if_goes_over_check = ($days_before - $days_before_thisMonth) + 1; } if($i == 1) { //$firstDay = ($firstDay > 4) ? $firstDay = 4 : $firstDay; for($fd = 0; $fd < $firstDay; $fd++, $counter++, $days_before_thisMonth++) { echo "<td>"; echo "<a href=\"?day=".$days_before_thisMonth."&month=".$month_before."&year=".$year_before."\" class=\"calendar-link\""; echo ">".$days_before_thisMonth."</a><br />"; } echo "</td>\r\n"; } } you can view this working here: http://troop557.vacau.com/calendar.php Edited May 1, 2013 by jack13580 Quote Link to comment https://forums.phpfreaks.com/topic/277485-calendar-math-logic/ Share on other sites More sharing options...
Barand Posted May 4, 2013 Share Posted May 4, 2013 You will need to explain the problem as I doubt anyone here is going to click on an external link posted by someone they don't know. Quote Link to comment https://forums.phpfreaks.com/topic/277485-calendar-math-logic/#findComment-1428239 Share on other sites More sharing options...
jack13580 Posted May 4, 2013 Author Share Posted May 4, 2013 (edited) Sorry about my horrible description, here's a revised one I am making my own custom calendar application/script and have just finished with one of its functions This function is to calculate the days of the month before the current month according to what day the current month started on and display them accordingly I was able to get it working, but it doesn't display the correct numbers all the time; some times it does and some times it doesn't you can view my calendar at http://troop557.vacau.com/calendar.php this can be vied by going to march and see that the last month is supposedly 31 days long but go to that month, February and its only 28 days long the code I came up with to get this to work is below, anyone know why its doing this? $numDays = date("t", $currentTimeStamp); $counter = 0; for($i = 1; $i < $numDays+1; $i++, $counter++){ $monthstring = $month; $monthlength = strlen($monthstring); $daystring = $i; $daylength = strlen($daystring); $timeStamp = strtotime("$year-$month-$i"); $timeStamp_before = strtotime("$year-$month_before-$i"); $month_before = ($month == 1) ? "12" : $month-1; $year_before = ($month == 1) ? $year-1 : $year; $firstDay = date("w", $timeStamp); $days_before = date("j", $timeStamp_before); $days_before_thisMonth = $days_before - $firstDay; $days_before_thisMonth = $days_before_thisMonth+1; for($if1=0;$if1!=1 && $i < 2;$if1++){ $if_goes_over_check = ($days_before - $days_before_thisMonth) + 1; } if($i == 1) { //$firstDay = ($firstDay > 4) ? $firstDay = 4 : $firstDay; for($fd = 0; $fd < $firstDay; $fd++, $counter++, $days_before_thisMonth++) { echo "<td>"; echo "<a href=\"?day=".$days_before_thisMonth."&month=".$month_before."&year=".$year_before."\" class=\"calendar-link\""; echo ">".$days_before_thisMonth."</a><br />"; } echo "</td>\r\n"; } } About the link to the calendar on my website, sorry that you feel that I'm a scumbag virus spreader but I would rather not want to have the risk of going to jail and would rather get better at building and designing websites and I really need help with this Edited May 4, 2013 by jack13580 Quote Link to comment https://forums.phpfreaks.com/topic/277485-calendar-math-logic/#findComment-1428264 Share on other sites More sharing options...
Solution Barand Posted May 6, 2013 Solution Share Posted May 6, 2013 try this <html> <head> <style type="text/css"> .calday { float:left; width:40px; height:40px; border:1px solid white; padding:5px; text-align:center; font-family: sans-serif; font-size: 7pt; background-color: #EEE; } </style> </head> <body> <?php $curMonth = 5; $curMonthDay1 = mktime(0,0,0,$curMonth,1); $dow1 = date('w', $curMonthDay1); $calStart = date('Y-m-d', strtotime("-$dow1 days", $curMonthDay1)); $daysInMonth = date('t', $curMonthDay1); $curMonthEnd = mktime(0,0,0,$curMonth,$daysInMonth); $dowEnd = 7-date('w', $curMonthEnd); $calEnd = date('Y-m-d', strtotime("+$dowEnd days", $curMonthEnd)); $d1 = new DateTime($calStart); $d2 = new DateTime($calEnd); $dp = new DatePeriod($d1, new DateInterval('P1D'), $d2); echo '<div style="width: 280px;">'; foreach ($dp as $d) { echo "<div class='calday'> {$d->format('M')}<br>{$d->format('d')} </div>\n"; } echo '</div>'; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/277485-calendar-math-logic/#findComment-1428515 Share on other sites More sharing options...
jack13580 Posted May 6, 2013 Author Share Posted May 6, 2013 Thanks, took me so long to figure out how to do this the first time and it didn't even work but with yours especially being so short just makes me sad but it does work I just had to change $dp to while($d1 < $d2) { $dp[] = $d1->format('Y-m-d'); $d1->modify('+1 day'); } because my free web host only has php 5.2.17 Quote Link to comment https://forums.phpfreaks.com/topic/277485-calendar-math-logic/#findComment-1428535 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.