maharge Posted February 5 Share Posted February 5 Hi, I am pretty new to this but am trying to add a set number of days (selected from a drop down) to a date selected in another field. $date1 = $values["validfrom"]; $date2 = $values["validto"]; $ndays = $values["validlength"]; $newdate = strtotime ( $date1. '+' .$nDays. $date2); $values["validto"]= date('Y-m-d', $newdate); Problem is, when I set the date to 1st February, I get 31st December 1969 in my validto field Can anyone help? Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/332557-adding-number-of-days-to-a-date/ Share on other sites More sharing options...
Barand Posted February 5 Share Posted February 5 I suspect strtodate() doesn't like the format passed to it (it expects a date string like "1 Feb 2026" and returns a Unix timestamp. If it fails it returns false which is giving the 31 Dec 1969 date. The Unix timestamps start from 1970-01-01.. I'd avoid timestamps and work with datetimes... $ndays = 14; $date1 = new DateTime('2026-02-01'); $ndays = new DateInterval("P{$ndays}D"); $date1->add($ndays); echo $date1->format('Y-m-d'); // 2026-02-15 Quote Link to comment https://forums.phpfreaks.com/topic/332557-adding-number-of-days-to-a-date/#findComment-1662454 Share on other sites More sharing options...
gizmola Posted February 6 Share Posted February 6 10 hours ago, maharge said: Can anyone help? Yes, use the technique that @Barand demonstrated. What is missing, and very important is your html form, with the specific field you are using. With HTML5 you can for example utilize an input with type="date". I would recommend using that, as well as html5 in general for your html pages, which is the current standard. While you can never fully trust user input to be valid, at least with HTML5 you can program to the assumptiion that the format of the date string that PHP receives from the form will be in RFC 3339/ISO 8601 format, which is YYYY-MM-dd. The format the user sees in the form will match their localization settings. When I say that you can not trust it, I mean that there are tools that can be used to submit form data directly without going through the HTML page, and for this reason, you always need to do additional validation on the backend. So you might want to validate that the date that has been submitted is infact of the format of 9999-99-99, and not some other format that will probably not work correctly, like 12/01/2023 or any other "string". If the user submits the data using your form, it will be correct, and will also be a "real" date, thanks to the built in browser handling of the "date" input type. Knowing this, you can expect that the date strings will work as Barand demonstrated in his code snippet. Quote Link to comment https://forums.phpfreaks.com/topic/332557-adding-number-of-days-to-a-date/#findComment-1662459 Share on other sites More sharing options...
phppup Posted March 5 Share Posted March 5 Additionally, you can elaborate on @gizmola comments by using drop-down inputs with one for month (variable A), one for date (variable B), and another for year (variable C). Then you can assemble the date as desired depending on the purpose (eg: CAB) with hyphens, dashes, and slashes seasoned in to your own taste. Quote Link to comment https://forums.phpfreaks.com/topic/332557-adding-number-of-days-to-a-date/#findComment-1663127 Share on other sites More sharing options...
gizmola Posted Sunday at 07:13 AM Share Posted Sunday at 07:13 AM On 3/5/2026 at 7:44 AM, phppup said: Additionally, you can elaborate on @gizmola comments by using drop-down inputs with one for month (variable A), one for date (variable B), and another for year (variable C). Then you can assemble the date as desired depending on the purpose (eg: CAB) with hyphens, dashes, and slashes seasoned in to your own taste. Certainly an option, although I think the HTML 5 date input is far superior to that from a user experience standpoint. It also has a built in Calendar popup, and you can pass various attributes that conform the date range as needed. I have taken advantage of this quite a bit, as I have had a need to set minimum/maximum date ranges, and these are all supported by the html5 input type. Quote Link to comment https://forums.phpfreaks.com/topic/332557-adding-number-of-days-to-a-date/#findComment-1663146 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.