Jump to content

Recommended Posts

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,

Link to comment
https://forums.phpfreaks.com/topic/332557-adding-number-of-days-to-a-date/
Share on other sites

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

 

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.  

  • 4 weeks later...

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.

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.