Simmo Posted March 31, 2011 Share Posted March 31, 2011 Hi, Something strange has happend today. I am from the UK the date is March 31st so I am using timezone: date_default_timezone_set('UTC'); I capture the date: $date = date("Y-m-d"); Add a month to the date: date("Y-m-d", strtotime("+1 Month", strtotime($date))) And its comming out as May. If I capture the date through the URL which I do when navigating back: $date = $_GET['date']; Its works and says April Can anyone please advise me on why this is happening. Thanks Link to comment https://forums.phpfreaks.com/topic/232274-keeps-adding-two-months-to-march-31st-instead-of-one/ Share on other sites More sharing options...
btherl Posted March 31, 2011 Share Posted March 31, 2011 Is date("Y-m-d") producing what you expect? Have you printed it out? Link to comment https://forums.phpfreaks.com/topic/232274-keeps-adding-two-months-to-march-31st-instead-of-one/#findComment-1194878 Share on other sites More sharing options...
Simmo Posted March 31, 2011 Author Share Posted March 31, 2011 Hi, Yes it is, any suggestions? Cheers Link to comment https://forums.phpfreaks.com/topic/232274-keeps-adding-two-months-to-march-31st-instead-of-one/#findComment-1194883 Share on other sites More sharing options...
salathe Posted March 31, 2011 Share Posted March 31, 2011 See this blog post from the guy who maintains PHPs date/time related functions: http://derickrethans.nl/obtaining-the-next-month-in-php.html Link to comment https://forums.phpfreaks.com/topic/232274-keeps-adding-two-months-to-march-31st-instead-of-one/#findComment-1194901 Share on other sites More sharing options...
Simmo Posted March 31, 2011 Author Share Posted March 31, 2011 Thanks for that, it all makes sense. Here is my solution: $month = date("m"); $year = date("Y"); date("Y-m-d", strtotime("+1 Month", strtotime(date("Y-m-d", strtotime($year.$month.'01'.' 00:00:00'))))) Link to comment https://forums.phpfreaks.com/topic/232274-keeps-adding-two-months-to-march-31st-instead-of-one/#findComment-1194940 Share on other sites More sharing options...
PFMaBiSmAd Posted March 31, 2011 Share Posted March 31, 2011 strtotime() and date() are fairly slow and you have a ton of extra ones. This is all you need - date("Y-m-d", strtotime(date('Y-m') . "-01 +1 Month")); Link to comment https://forums.phpfreaks.com/topic/232274-keeps-adding-two-months-to-march-31st-instead-of-one/#findComment-1194947 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.