Jump to content

calendar math logic


jack13580

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/277485-calendar-math-logic/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/277485-calendar-math-logic/#findComment-1428264
Share on other sites

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>
Link to comment
https://forums.phpfreaks.com/topic/277485-calendar-math-logic/#findComment-1428515
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/277485-calendar-math-logic/#findComment-1428535
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.