Johnain Posted October 12, 2009 Share Posted October 12, 2009 Hi all I am constantly baffled by the unbelievable difficulty of carrying out simple date manipulations in PHP. I have just spent around six hours trying to add a day to a date and I still do not have the answer. I have a date stored in a date filed in my MySQL database in the format YYYY-DD-MM. All I want to do is retrieve it, add a day to it and display it in an HTML form as DD-MM-YYYY. I already have a function that will reverse it to DD-MM-YYYY for display and another that will reverse it back to my YYYY-MM-DD format when I wish to write it back to the database, so it's just adding a day (or two days, or any other number of days) that I need to do. But the apparently simple task of adding one (or more) days to it has proved beyond me. I have scoured the web and seen almost every solution known to mankind. Most of them incredibly complex. It must be me being an idiot. Can anybody help, please. My latest attemp is this ... function calcdate($indate, $noofdays) { // calculates a number of days from the supplied date, plus (no sign needed) or minus (give - sign in $noofdays // Assumes that indate is yyyy-mm-dd and the outdate will be in the same format. $date = strtotime(date("Y-m-d", strtotime($indate)) . " +".$noofdays ." day"); return $date ; } But it returns a number ! Quote Link to comment https://forums.phpfreaks.com/topic/177393-solved-manipulating-dates/ Share on other sites More sharing options...
Johnain Posted October 12, 2009 Author Share Posted October 12, 2009 Yikes cracked it - after 6 hours. Yippee. The solution (simple as always) was ... function calcdate($indate, $noofdays) { // calculates a number of days from the supplied date, plus (no sign needed) or minus (give - sign in $noofdays // Assumes that indate is yyyy-mm-dd and the outdate will be in the same format. $date = strtotime(date("Y-m-d", strtotime($indate)) . " +".$noofdays ." day"); return date("Y-m-d", $date) ; } Quote Link to comment https://forums.phpfreaks.com/topic/177393-solved-manipulating-dates/#findComment-935327 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.