arunpatal Posted April 5, 2014 Share Posted April 5, 2014 Hi, This is month (example november) $month = date('m'); This is month + 1 (example december) $month = date('m') + 1; Now adding + 2 shows 13 $month = date('m') + 2; This is orignal code if($month == 13): $month = sprintf("%02s", 1); $year++; endif; It make 13 into 1 but then it repeats again from 14 13 becomes 1 then 14 stays 14 how can i make month start from 1 till 12 with the help of loop.... ofcourse one bad solution is that i doi it with if statement like if ($month == 14){$month = 2} if ($month == 15){$month = 3} Is there a way to do it via loop ??? Quote Link to comment Share on other sites More sharing options...
possien Posted April 5, 2014 Share Posted April 5, 2014 Are you just trying to return $month plus 1 and if $month is 13 it becomes 1? $month = date('m'); function month($month){ $month = ++$month; if($month >12)$month = 1; return $month; } echo month($month); Quote Link to comment Share on other sites More sharing options...
arunpatal Posted April 5, 2014 Author Share Posted April 5, 2014 Are you just trying to return $month plus 1 and if $month is 13 it becomes 1? $month = date('m'); function month($month){ $month = ++$month; if($month >12)$month = 1; return $month; } echo month($month); I will try it and will get back to it.... thanks for example Quote Link to comment Share on other sites More sharing options...
arunpatal Posted April 5, 2014 Author Share Posted April 5, 2014 (edited) I will try it and will get back to it.... thanks for example Are you just trying to return $month plus 1 and if $month is 13 it becomes 1? $month = date('m'); function month($month){ $month = ++$month; if($month >12)$month = 1; return $month; } echo month($month); after 12 it display all month as 1 if($month >12)$month = 1; i need to make a loop... somthing like this if month is 13 then it start from 1 again till 12 and when month is 25 then again 1 till 12... and so on Edited April 5, 2014 by arunpatal Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 5, 2014 Share Posted April 5, 2014 if the month is greater than 12, you would subtract 12, i.e. 13 -12 = 1, 14 - 12 = 2, .... Quote Link to comment Share on other sites More sharing options...
possien Posted April 5, 2014 Share Posted April 5, 2014 If you are extracting month from date('m') it's never going to be greater than 12? Quote Link to comment Share on other sites More sharing options...
arunpatal Posted April 6, 2014 Author Share Posted April 6, 2014 (edited) If you are extracting month from date('m') it's never going to be greater than 12? I have a calander which i make start like this date('m')... now, i want to add 2 years calander(24 month from current month) for which i make date('m') + 1 ..... date('m') + 23... that is why the month is going 13,14,15...... Edited April 6, 2014 by arunpatal Quote Link to comment Share on other sites More sharing options...
possien Posted April 6, 2014 Share Posted April 6, 2014 (edited) I think I understand now. If you have for example $month = 36, that would be 2 years and 12 months, the maximum you need. So you would need to find the years and months up to that value: $month= 25; function month($month){ if($month < 13) { $month = $month; $year = 0; } elseif($month < 25){ $month = $month-12; $year = 1; } elseif($month < 37 ){ $month = $month-24; $year = 2; } echo 'Month: '.$month.'<br>'; echo 'Year: '.$year; } month($month); So, is this what you are trying to do? You can add $year to the current or other date plus get the month. THis evaluates from the lowest to highest value of $month. Edited April 6, 2014 by possien Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 6, 2014 Share Posted April 6, 2014 to do arbitrary date math, use the datetime class as it will handle the rollover in the year value for you. Quote Link to comment Share on other sites More sharing options...
possien Posted April 6, 2014 Share Posted April 6, 2014 to do arbitrary date math, use the datetime class as it will handle the rollover in the year value for you. Thats true, you can do a simple date difference and return an array containing year and month. (see the PHP manual). Not sure what he is trying to do. Quote Link to comment 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.