Senthilkumar Posted June 6, 2023 Share Posted June 6, 2023 Dear Team, I am having a input field which is using for select month. <?PHP $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. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 6, 2023 Share Posted June 6, 2023 $previous_month = date('Y-m', strtotime("-1 month")); Quote Link to comment Share on other sites More sharing options...
requinix Posted June 6, 2023 Share Posted June 6, 2023 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)); Quote Link to comment Share on other sites More sharing options...
maxxd Posted June 7, 2023 Share Posted June 7, 2023 (edited) 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 June 7, 2023 by maxxd Quote Link to comment Share on other sites More sharing options...
Senthilkumar Posted June 7, 2023 Author Share Posted June 7, 2023 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 7, 2023 Share Posted June 7, 2023 4 hours ago, maxxd said: Or, if you just really like to over-engineer things: Same problem: March 30th - 1 month = February 30th -> March 2nd. Quote Link to comment Share on other sites More sharing options...
Senthilkumar Posted June 7, 2023 Author Share Posted June 7, 2023 (edited) Hello requinix I used the below code as per your instruction $previous_month = date('Y-m', mktime(0, 0, 0, date('n') - 1, 1)); But than also the same problem Edited June 7, 2023 by Senthilkumar Quote Link to comment Share on other sites More sharing options...
maxxd Posted June 7, 2023 Share Posted June 7, 2023 6 hours ago, requinix said: Same problem: March 30th - 1 month = February 30th -> March 2nd. Well damn - I had in my head that it accounted for that. I was wrong. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 7, 2023 Share Posted June 7, 2023 Alternative? date('Y-m', strtotime("first day of last month")); Quote Link to comment Share on other sites More sharing options...
maxxd Posted June 8, 2023 Share Posted June 8, 2023 Continuing a long history of over-engineering because it reads easier to my particular brain: echo (new DateTime('first day of last month', new DateTimeZone('America/New_York')))->format('M j, Y'); Returns 'May 1, 2023' which, as of today, is correct. Thanks @Barand and @requinix. 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.