CalcBuilder Forum

Add day

User mehmethocam1 2017-03-31 12:47:06

Hello,
Frontend Date format is mm/dd/yy (variable baslangic)
I want to add to 40 day to my variable.
How can I?


// Not correct
$dayf=40;
$newDate = new JDate($baslangic .' +'.$dayf.' day');
$result= $newDate->format('m/d/Y');

Thanks

Moonsoft support 2017-03-31 13:49:53

Hello,
you can try to use php datetime class in order to perform your function, the code should look like:


$dayf=40;
//create date object from format
$newDate = DateTime::createFromFormat('m/d/y', $baslangic);
//add interval
$newDate->modify('+'.$dayf.' days');
//return formatted new date
$result=$newDate->format('m/d/y');

That should do the work, format to be used here is 'm/d/y' if you use 2 digits for year, or,

'm/d/Y'

in case you use the 4 digits format.

Hope this helps, regards

User mehmethocam1 2017-03-31 14:24:29

Thanks support.

Code produces incorrect results.

Sample baslangic is 01/01/2017
Result: 10/02/2017 This is correct

Sample baslangic is 30/01/2017
Result: 11/07/2019 This is not correct.


I can tired code:

$dayf=40;
//create date object from format
$newDate = DateTime::createFromFormat('m/d/Y', $baslangic);
//add interval
$newDate->modify('+'.$dayf.' days');
//return formatted new date
$result=$newDate->format('m/d/Y');

Moonsoft support 2017-03-31 16:37:32

Hi,
are you sure the date format is the right one? You said you're using mm/dd/yy format, but from your example:

30/01/2017

seems you're using dd/mm/yy instead? If so, you only need to correct the date format to

d/m/Y

at the 2 date functions (createfromformat and format) in order to get the right results. If the format is not the exact one you'll be getting weird unexpected results when adding the days.

Regards

User mehmethocam1 2017-04-02 18:15:39

Thanks, It is work.
You are perfect.

...
Support/development 40 hours

With the peace of mind of having a professional team at your service (20% discount)

Buy now!
...
CalcBuilder

Create dynamic calculators

Buy now!