sheraz Posted September 30, 2010 Share Posted September 30, 2010 hi, i am working on an assignment in which i am getting current date and then getting date of two previous months i.e 2010-09 2010-08 2010-07 the problem is that the code is working perfectly on localhost but when i upload the file it only shows the current date and do not subtract and display dates. can some body guide me that what is the problem and how to solve it. :-\ here is the code $date1= date("Y-m"); $date2 = strtotime ( '-1 month' , strtotime ( $date1 ) ) ; $date2 = date ( 'Y-m' , $date2 ); $date3 = strtotime('-1 month', strtotime( $date2 ) ); $date3 = date( 'Y-m' , $date3); ?> <select name=""> <option><?php echo $date1; ?></option> <option><?php echo $date2; ?></option> <option><?php echo $date3; ?></option> </select> Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 30, 2010 Share Posted September 30, 2010 I suspect the problem is a difference between the date/time settings on your PC vs the Server. For example, in the US Jan. 5th, 2010 would be represented as 1-5-2010, whereas in most European countries it would be represented as 5-1-2010. So, I imagine your PC's settings recognizes "2010-09" as September 2010 and the settings on the Server do not. But, all of that can be solved by removing the overcomplication from what you have. You are converting dates back and forth unneccessarily. Try this: <select name=""> <option><?php echo date("Y-m"); ?></option> <option><?php echo date("Y-m", strtotime("-1 month")); ?></option> <option><?php echo date("Y-m", strtotime("-2 months")); ?></option> </select> Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 30, 2010 Share Posted September 30, 2010 Or, for a better programatical solution you could use the code below. That way you can easily modify how many options are displayed in the list by modifying the value of $numberOfMonthsToDisplay. <?php $numberOfMonthsToDisplay = 3; $dateOptions = ''; for($monthsBack=0; $monthsBack<$numberOfMonthsToDisplay; $monthsBack++) { $dateValue = date("Y-m", strtotime("-{$monthsBack} month")); $dateOptions .= " <option value=\"{$dateValue}\">{$dateValue}</option>\n"; } ?> <select name=""> <?php echo $dateOptions; ?> </select> Quote Link to comment Share on other sites More sharing options...
sheraz Posted September 30, 2010 Author Share Posted September 30, 2010 this code works ok but when i set the system date to January 2010 it displays 2010-01 2009-12 2009-12 please check it Thanks :-\ Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 30, 2010 Share Posted September 30, 2010 I really like helping people, but c'mon could you not take 5 minutes to figure out the problem yourself? You need to learn how to effectively debug code if you are going to program. You didn't even provide enough information for me to reproduce the defect. You should have stated that you set the date to Jan 31st since that was the only date in January that caused the problem. And, I was easily able to discover why by just modifying the $dateValue to $dateValue = date("Y-m-d", strtotime("-{$monthsBack} month")); I then saw the results as 2010-01-31 2009-12-31 2009-12-01 I don't know if this is a PHP bug or not. But, instead of trying to fight it a simple code change would fix it. Just force the base date to be the first day of the current month. <?php $numberOfMonthsToDisplay = 3; $dateOptions = ''; for($monthsBack=0; $monthsBack<$numberOfMonthsToDisplay; $monthsBack++) { $firstDayOfMonth = date("Y-m-1"); $dateValue = date("Y-m", strtotime("$firstDayOfMonth -{$monthsBack} months")); $dateOptions .= " <option value=\"{$dateValue}\">{$dateValue}</option>\n"; } ?> <select name=""> <?php echo $dateOptions; ?> </select> Quote Link to comment Share on other sites More sharing options...
sheraz Posted October 1, 2010 Author Share Posted October 1, 2010 sorry for lake of information. now i have modify the code its now working correctly on localhost. the problem is that we are uploading it to linux server. so i think that the problem is of date format as you also said before. 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.