Jump to content

Repeating month


arunpatal

Recommended Posts

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 ???

Link to comment
https://forums.phpfreaks.com/topic/287546-repeating-month/
Share on other sites

  On 4/5/2014 at 8:49 PM, possien said:

 

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 :)

Link to comment
https://forums.phpfreaks.com/topic/287546-repeating-month/#findComment-1475068
Share on other sites

  On 4/5/2014 at 9:22 PM, arunpatal said:

I will try it and will get back to it....

thanks for example :)

 

  On 4/5/2014 at 8:49 PM, possien said:

 

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

Link to comment
https://forums.phpfreaks.com/topic/287546-repeating-month/#findComment-1475070
Share on other sites

  On 4/5/2014 at 10:42 PM, possien said:

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......

Link to comment
https://forums.phpfreaks.com/topic/287546-repeating-month/#findComment-1475100
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/287546-repeating-month/#findComment-1475150
Share on other sites

  On 4/6/2014 at 6:31 PM, mac_gyver said:

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.

Link to comment
https://forums.phpfreaks.com/topic/287546-repeating-month/#findComment-1475154
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.