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
Share on other sites

 

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
Share on other sites

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 by arunpatal
Link to comment
Share on other sites

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 by arunpatal
Link to comment
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.

Edited by possien
Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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