Jump to content

Not able to select previous month on mobile web browser


Recommended Posts

Dear Team,

I am having a input field which is using for select month. 

  $previous_month = date('Y-m', strtotime(date('Y-m') . " -1 month"));
        $current_month = date('Y-m');


 <input type="month" name="Month" id="Month" class="form-control" onchange="viewData()" min="<?php echo $previous_month; ?>" max="<?php echo $current_month; ?>" required style="width:auto;font-family:Cambria;border-radius:5px;height:30px" />

This input will display previous month and cutrrent month only. 

When i am checking the output of this input is working properly on my laptop browser, i can able to select both the month. But when ia m checking the output on my mobile browser, i can select only current month. ie.. max value. If i scroll down the month list, again it is going to max value only.

If suppose the minimum value is -2 (ie.. 2month ) then i can able to select on my mobile browser. But when i am ggiven one month i am not able to select previous month.


Can any one suggest me where is the mistake and how to rectify this issue.

Link to comment
Share on other sites

9 hours ago, Barand said:
$previous_month = date('Y-m', strtotime("-1 month"));


Careful with dates like March 30th...

Easiest method I know: date + mktime.

$previous_month = date('Y-m', mktime(0, 0, 0, date('n') - 1, 1));


Link to comment
Share on other sites

Or, if you just really like to over-engineer things:

echo (new DateTime('now', new DateTimezone('America/New_York')))->sub(new DateInterval('P1M'))->format('M j, Y');

As dumb as it may sound given the length of this statement versus requinix and Barand's answers, this is easier for my brain to read.

Edited by maxxd
Link to comment
Share on other sites

I changed the code as per your instruction. But when i am selecting the month from system browser it is displaying like below image



When i am selecting month from mobile browser it is displaying like below image


In above screen, when i am scroll down the month i am not able to select May month. Automatically it is going to Jun if i scroll it.


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.

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.