lanu Posted December 31, 2011 Share Posted December 31, 2011 I'm creating an alternative calendar system(non-gregorian) and so I need to know some basic math to figure some of this out and how to express it in php. the calendar year is 364 days long and it is split into 14 months that are 26 days long each. I have create a script that determines which day of 26 no problem, but getting the month is proving difficult. My code I'm using to display the content for each of the 26 days is as follows: <?php if ($beyondportal % 26 == $day1 || abs($beyondportal) % 26 == $day1){ ?> It's day 1 <?php } ; ?> <?php if ($beyondportal % 26 == $day2 || abs($beyondportal) % 26 == $day2){ ?> It's day 2 <?php } ; ?> etc... up to 26 here is the code that gives $beyondportal and the day variables their values <?php // gets todays date and compares it to 11.11.11, if its positive days get a certain set of values, if negative they get another $elvenportal = mktime(11,11,11,11,11,2011); $now = mktime(11,11,11,date("m"),date("d"),date("y")); $late = date("H"); if ($late < 5) {$now = mktime(11,11,11,date("m"),date("d")-1,date("y"));}; if ($_POST["dayz"] != "" ){ if ($_POST["month"] != "" ){ if ($_POST["year"] != "" ){ $now = mktime(11,11,11,$_POST["month"],$_POST["dayz"],$_POST["year"]); };};}; $day1 = 22;$day2 = 23;$day3 = 24;$day4 = 25;$day5 = 0;$day6 = 1;$day7 = 2;$day8 = 3;$day9 = 4;$day10 = 5;$day11 = 6;$day12 = 7;$day13 = 8;$day14 = 9;$day15 = 10;$day16 = 11;$day17 = 12;$day18 = 13;$day19 = 14;$day20 = 15;$day21 = 16;$day22 = 17;$day23 = 18;$day24 = 19;$day25 = 20;$day26 = 21;$cycle5=0;$cycle6=1;$cycle7=2;$cycle8=3;$cycle9=4;$cycle10=5;$cycle11=6;$cycle12=7;$cycle13=8;$cycle14=9;$cycle1=10;$cycle2=11;$cycle3=12;$cycle4=13; if($now < $elvenportal){ $day1 = 4;$day2 = 3;$day3 = 2;$day4 = 1;$day5 = 0;$day6 = 25;$day7 = 24;$day8 = 23;$day9 = 22;$day10 = 21;$day11 = 20;$day12 = 19;$day13 = 18;$day14 = 17;$day15 = 16;$day16 = 15;$day17 = 14;$day18 = 13;$day19 = 12;$day20 = 11;$day21 = 10;$day19 = 12;$day22 = 9;$day23 = 8;$day24 = 7;$day25 = 6;$day26 = 5; $cycle5=0;$cycle6=13;$cycle7=12;$cycle8=11;$cycle9=10;$cycle10=9;$cycle11=8;$cycle12=7;$cycle13=6;$cycle14=5;$cycle1=4;$cycle2=3;$cycle3=2;$cycle4=1; }; $offset = $now-$elvenportal; $beyondportal = FLOOR($offset/60/60/24) ; ?> Now I tried to do a similar thing for the cycles like so: <?php if ($beyondportal % 364 == $cycle1 || abs($beyondportal) % 364 == $cycle1){ ?> <h4>Cycle 1</h4> <?php } ; ?> <?php if ($beyondportal % 364 == $cycle2 || abs($beyondportal) % 364 == $cycle2){ ?> <h4>Cycle 2</h4> <?php } ; ?> etc... up to 14 but this seems not to be the right approach any help would be really great thank you all Quote Link to comment https://forums.phpfreaks.com/topic/254139-blocks-of-26-numbers-every-364/ Share on other sites More sharing options...
Pikachu2000 Posted December 31, 2011 Share Posted December 31, 2011 When posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/254139-blocks-of-26-numbers-every-364/#findComment-1302900 Share on other sites More sharing options...
lanu Posted December 31, 2011 Author Share Posted December 31, 2011 sorry about that here's the code I'm creating an alternative calendar system(non-gregorian) and so I need to know some basic math to figure some of this out and how to express it in php. the calendar year is 364 days long and it is split into 14 months that are 26 days long each. I have create a script that determines which day of 26 no problem, but getting the month is proving difficult. My code I'm using to display the content for each of the 26 days is as follows: <?php if ($beyondportal % 26 == $day1 || abs($beyondportal) % 26 == $day1){ ?> It's day 1 <?php } ; ?> <?php if ($beyondportal % 26 == $day2 || abs($beyondportal) % 26 == $day2){ ?> It's day 2 <?php } ; ?> etc... up to 26 here is the code that gives $beyondportal and the day variables their values <?php // gets todays date and compares it to 11.11.11, if its positive days get a certain set of values, if negative they get another $elvenportal = mktime(11,11,11,11,11,2011); $now = mktime(11,11,11,date("m"),date("d"),date("y")); $late = date("H"); if ($late < 5) {$now = mktime(11,11,11,date("m"),date("d")-1,date("y"));}; if ($_POST["dayz"] != "" ){ if ($_POST["month"] != "" ){ if ($_POST["year"] != "" ){ $now = mktime(11,11,11,$_POST["month"],$_POST["dayz"],$_POST["year"]); };};}; $day1 = 22;$day2 = 23;$day3 = 24;$day4 = 25;$day5 = 0;$day6 = 1;$day7 = 2;$day8 = 3;$day9 = 4;$day10 = 5;$day11 = 6;$day12 = 7;$day13 = 8;$day14 = 9;$day15 = 10;$day16 = 11;$day17 = 12;$day18 = 13;$day19 = 14;$day20 = 15;$day21 = 16;$day22 = 17;$day23 = 18;$day24 = 19;$day25 = 20;$day26 = 21;$cycle5=0;$cycle6=1;$cycle7=2;$cycle8=3;$cycle9=4;$cycle10=5;$cycle11=6;$cycle12=7;$cycle13=8;$cycle14=9;$cycle1=10;$cycle2=11;$cycle3=12;$cycle4=13; if($now < $elvenportal){ $day1 = 4;$day2 = 3;$day3 = 2;$day4 = 1;$day5 = 0;$day6 = 25;$day7 = 24;$day8 = 23;$day9 = 22;$day10 = 21;$day11 = 20;$day12 = 19;$day13 = 18;$day14 = 17;$day15 = 16;$day16 = 15;$day17 = 14;$day18 = 13;$day19 = 12;$day20 = 11;$day21 = 10;$day19 = 12;$day22 = 9;$day23 = 8;$day24 = 7;$day25 = 6;$day26 = 5; $cycle5=0;$cycle6=13;$cycle7=12;$cycle8=11;$cycle9=10;$cycle10=9;$cycle11=8;$cycle12=7;$cycle13=6;$cycle14=5;$cycle1=4;$cycle2=3;$cycle3=2;$cycle4=1; }; $offset = $now-$elvenportal; $beyondportal = FLOOR($offset/60/60/24) ; ?> Now I tried to do a similar thing for the cycles like so: <?php if ($beyondportal % 364 == $cycle1 || abs($beyondportal) % 364 == $cycle1){ ?> <h4>Cycle 1</h4> <?php } ; ?> <?php if ($beyondportal % 364 == $cycle2 || abs($beyondportal) % 364 == $cycle2){ ?> <h4>Cycle 2</h4> <?php } ; ?> etc... up to 14 but this seems not to be the right approach any help would be really great thank you all Quote Link to comment https://forums.phpfreaks.com/topic/254139-blocks-of-26-numbers-every-364/#findComment-1302903 Share on other sites More sharing options...
kicken Posted January 2, 2012 Share Posted January 2, 2012 Perhaps this will help: <?php $ts = mktime(11, 11, 11, 11, 11, 2011); $dayOfYear = date('z', $ts); $cycle = intval($dayOfYear/26); $day = $dayOfYear % 26; echo '<h1>Cycle '.$cycle.'</h1>'; echo '<p>Day: '.$day.'</p>'; Quote Link to comment https://forums.phpfreaks.com/topic/254139-blocks-of-26-numbers-every-364/#findComment-1303397 Share on other sites More sharing options...
Zane Posted January 2, 2012 Share Posted January 2, 2012 Wouldn't something like this be a little more efficient? $days = range(1, 364); $months = array_chunk($days, 26); $currentMonth = 0; $currentDay = 253; foreach ($months as $month => $daylist) { $checkMonth = array_search ($currentDay, $daylist); if ( $checkMonth !== false) { $currentMonth = key($month) + 1; break 1; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254139-blocks-of-26-numbers-every-364/#findComment-1303414 Share on other sites More sharing options...
kicken Posted January 2, 2012 Share Posted January 2, 2012 Wouldn't something like this be a little more efficient? Than a simple modulus and division? Not likely. Running loop method: Method ran in 1.3520817756653 seconds int(10) int(20) Running math method: Method ran in 0.061004877090454 seconds int(10) int(20) when performing 100,000 iterations. Quote Link to comment https://forums.phpfreaks.com/topic/254139-blocks-of-26-numbers-every-364/#findComment-1303427 Share on other sites More sharing options...
lanu Posted January 2, 2012 Author Share Posted January 2, 2012 This almost works for me but I'm really unsure about the math part: <?php $zeropoint = mktime(11, 11, 11, 06, 29, 2011); // zero date has been moved 135 days earlier to june 29th,2011 - cycle 1, day 1 - the new year of the calendar to simplify this $now = mktime(11,11,11,date("m"),date("d"),date("y")); // gets todays date $diff = $now - $zeropoint; // do i need to do another condition incase $now is less than zeropoint or will this work? $cycle = $diff % 364 ; // no idea what the math is supposed to be to determine which of the 14 cycles we are currently in based on the difference between $zeropiont and $now $day = $diff % 26 ; // no idea what the math is supposed to be to determine which of the 26 days we are currently in based on the difference between $zeropiont and $now echo 'Cycle '.$cycle.' '; echo 'Day: '.$day.' '; ?> Thank you all so much for your help it is greatly appreciated and encouraging! Quote Link to comment https://forums.phpfreaks.com/topic/254139-blocks-of-26-numbers-every-364/#findComment-1303478 Share on other sites More sharing options...
kicken Posted January 3, 2012 Share Posted January 3, 2012 $diff = $now - $zeropoint; // do i need to do another condition incase $now is less than zeropoint or will this work? You can wrap in in abs() to ensure it is positive. $cycle = $diff % 364 ; // no idea what the math is supposed to be to determine which of the 14 cycles we are currently in based on the difference between $zeropiont and $now $diff is in # of seconds, so your statement is not going to work. It's also not correct, look at what I did above. The cycles is gathered via integer division. Since your interested in days, you'd be best served by converting $diff to days first. $days = $diff/(60*60*24); $cycle = intval($days / 26)+1; //26 days in your cycles. +1 to make it 1-14 rather than 0-13 The current day you get via the modulus operator which returns the remainder of a division operation $day = $diff % 26 + 1; //26 days in the cycle. +1 to make it 1-26 rather than 0-25 Modulus returns the number value past the last evenly divisible number. So say your $diff is 176 (days). 176/26 = 6.769 So we drop the decimals and multiple the integer with 26, to get 26*6 = 156 Then we take that and subtract it from 176, for: 176-156=20 So modulus will return 20. So for $diff=176 (days) we get: $cycle = intval(176/26)+1 = 7; $day = 176 % 26 + 1 = 21; So you are on cycle 7, day 21. Quote Link to comment https://forums.phpfreaks.com/topic/254139-blocks-of-26-numbers-every-364/#findComment-1303513 Share on other sites More sharing options...
lanu Posted January 3, 2012 Author Share Posted January 3, 2012 almost got it now. it works great going forward but backward has some issues. if beyond portal is a date from the past we run into a problem because 1 day BEFORE 0 is actually DAY 26 where as 1 day after is DAY 2. My problem is I'm getting DAY 2 when it is supposed to be getting DAY 26 and likewise any dates before 11.11.11 are being counted wrong. any suggestions? here's the code: $elvenportal = mktime(11,11,11,11,11,2011); $now = mktime(date("H"),date("i"),date("s"),date("n"),date("j"),date("Y")); $offset = $now - $elvenportal; $beyondportal = abs($offset/(60*60*24)); $beyondportal = $beyondportal+108; // start of new year is 108 days before 11.11.11 $day = $beyondportal % 26 + 1; //26 days in the cycle. +1 to make it 1-26 rather than 0-25 $cycle = intval($beyondportal / 26) % 14+1; //26 days in your cycles. +1 to make it 1-14 rather than 0-13 // do I need a conditional statement to calculate differently for negative numbers? ANy help would be greatly appreciated. thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/254139-blocks-of-26-numbers-every-364/#findComment-1303878 Share on other sites More sharing options...
lanu Posted January 4, 2012 Author Share Posted January 4, 2012 I got it to work, though not the most elegant solution, it does the job for my purposes. I have given each of the days different values in the case that its a date earlier than 11.11.11 so the days and cycles match up forwards and backwards. Thanks again for all your help I LOOOOVE PHP FREAKS! <?php // THEE AURON CALCULATOR $elvenportal = mktime(11,11,11,11,11,2011); $now = mktime(date("H"),date("i"),date("s"),date("n"),date("j"),date("Y")); $late = date("H"); $theeday = date("d")-1; if ($late < 7) {$now = mktime(11,11,11,date("n"),$theeday,date("Y"));}; if ($_POST["dayz"] != "" ){ if ($_POST["month"] != "" ){ if ($_POST["year"] != "" ){ $now = mktime(11,11,11,$_POST["month"],$_POST["dayz"],$_POST["year"]); };};}; $offset = $now - $elvenportal; $beyondportal = abs($offset/(60*60*24)); $beyondportal = abs($beyondportal+108); $day = abs($beyondportal % 26 + 1); //26 days in the cycle. +1 to make it 1-26 rather than 0-25 $cycle = abs(intval($beyondportal / 26) % 14+1); //26 days in your cycles. +1 to make it 1-14 rather than 0-13 if ($elvenportal > $now){ if ($day == 6){$day = 4;} else if ($day == 7){$day = 3;} else if ($day == {$day = 2;} else if ($day == 9){$day = 1;} else if ($day == 10){$day = 26;} else if ($day == 11){$day = 25;} else if ($day == 12){$day = 24;} else if ($day == 13){$day = 23;} else if ($day == 14){$day = 22;} else if ($day == 15){$day = 21;} else if ($day == 16){$day = 20;} else if ($day == 17){$day = 19;} else if ($day == 18){$day = 18;} else if ($day == 19){$day = 17;} else if ($day == 20){$day = 16;} else if ($day == 21){$day = 15;} else if ($day == 22){$day = 14;} else if ($day == 23){$day = 13;} else if ($day == 24){$day = 12;} else if ($day == 25){$day = 11;} else if ($day == 26){$day = 10;} else if ($day == 1){$day = 9;} else if ($day == 2){$day = 8;} else if ($day == 3){$day = 7;} else if ($day == 4){$day = 6;} else if ($day == 5){$day = 5;} else if ($day == 6){$day = 4;}; if ($beyondportal > 4){ if ($cycle == 1){$cycle = 8;} else if ($cycle == 2){$cycle = 7;} else if ($cycle == 3){$cycle = 6;} else if ($cycle == 4){$cycle = 5;} else if ($cycle == 5){$cycle = 4;} else if ($cycle == 6){$cycle = 3;} else if ($cycle == 7){$cycle = 2;} else if ($cycle == {$cycle = 1;} else if ($cycle == 9){$cycle = 14;} else if ($cycle == 10){$cycle = 13;} else if ($cycle == 11){$cycle = 12;} else if ($cycle == 12){$cycle = 11;} else if ($cycle == 13){$cycle = 10;} else if ($cycle == 14){$cycle = 9;}; }; /* if ($day == 1){$day = 5;} else if ($day == 2){$day = 4;} else if ($day == 3){$day = 3;} else if ($day == 4){$day = 2;} else if ($day == 5){$day = 1;} else if ($day == 6){$day = 26;} else if ($day == 7){$day = 25;} else if ($day == {$day = 24;} else if ($day == 9){$day = 23;} else if ($day == 10){$day = 22;} else if ($day == 11){$day = 21;} else if ($day == 12){$day = 20;} else if ($day == 13){$day = 19;} else if ($day == 14){$day = 18;} else if $day == 15){$day = 17;} else if ($day == 16){$day = 16;} else if ($day == 17){$day = 15;} else if ($day == 18){$day = 14;} else if ($day == 19){$day = 13;} else if ($day == 20){$day = 12;} else if ($day == 21){$day = 11;} else if ($day == 22){$day = 10;} else if ($day == 23){$day = 9;} else if ($day == 24){$day = 8;} else if ($day == 25){$day = 7;} else if ($day == 26){$day = 6;} else{echo "";} if ($cycle = 5){$cycle = 1;} else if ($cycle = 6){$cycle = 14;} else if ($cycle = 7){$cycle = 13;} else if ($cycle = {$cycle = 12;} else if ($cycle = 9){$cycle = 11;} else if ($cycle = 10){$cycle = 10;} else if $cycle = 11){$cycle = 9;} else if ($cycle = 12){$cycle = 8;} else if ($cycle = 13){$cycle = 7;} else if ($cycle = 14){$cycle = 6;} else if ($cycle = 1){$cycle = 5;} else if ($cycle = 2){$cycle = 4;} else if ($cycle = 3){$cycle = 3;} else if ($cycle = 4){$cycle = 2;} else{echo "";}*/ }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254139-blocks-of-26-numbers-every-364/#findComment-1303935 Share on other sites More sharing options...
Pikachu2000 Posted January 4, 2012 Share Posted January 4, 2012 Just a couple of notes, if you're interested. For the $now variable, you don't need to call date() 6 times within mktime(). It's a relatively slow function to begin with; you can simply use $now = time(). For the two stacks of if/elseif conditionals, you might find it cleaner to use lookup arrays to pull the values from. There's nothing particularly wrong with the way you have it now; it's just a thought in case you want less lines of code. $lookup_days = array( 1 => 9, 8, 7, 6, 5, 4, 3, 2, 1, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10 ); if ($elvenportal > $now){ $day = $lookup_days[$day]; } Quote Link to comment https://forums.phpfreaks.com/topic/254139-blocks-of-26-numbers-every-364/#findComment-1303944 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.