Date conversion is not correct.

Hello team,

From last day I am facing problem of date addition in the form application. On start date blur event, I want to add duration such that end date should obtain and here is my code.


StartDate_onBlur:

if({StartDate} != null)
{
	// sc_date(date, format, operator, D,M,Y)
	// $date=sc_date({StartDate}, "ddmmyyyy", "+", {Duration}, 0, 0);
	// {EndDate}=$date;
	
	// {EndDate} = sc_date({StartDate}, "dd-mm-yyyy", "+", 30, 0, 0);
	// sc_error_message({EndDate});
	
	$dt = {StartDate};
	$cd = strtotime($dt);
	$newDate = date("d-m-Y", mktime(0,0,0,date("m", $cd),date("d", $cd),date("Y", $cd)));
	{EndDate} = $newDate;
}

I am getting some output but it is not correct such as 03-08-2013 + 129 = 69-19-3112. Where do it going wrong?


Milind

If you want to add days to a date the easiest way is: echo date(‘Y-m-d’, strtotime("+30 days"));

The textstring for addition can be created by php. Hope this helps.

Thanks Albert,

This post help me, but I want to add days to start date not to current date. I have start date, end date and duration columns. Lets say a task xyz takes 45 days to complete and I will schedule this task at any future date, then it should automatically give me end date. Now in the above example, duration is adding into current date.

One way can be calculation duration days from start date to current date and add them in duration, but this is bad idea. Can we add duration to start date to obtain end date with very simple way? Or do I let database to do this calculation?


Milind.

You can use any startdate. A quick sample from the internet:

$date = “04-15-2013”;
$date1 = str_replace(’-’, ‘/’, $date);
$tomorrow = date(‘m-d-Y’,strtotime($date1 . “+1 days”));

echo $tomorrow;

Thank you Albert. What I did is like this and it works and very simple to do.


if(isset({StartDate}))
{
	$query = "SELECT DATE_ADD('{StartDate}', INTERVAL {Duration} DAY)";
	sc_lookup(enddate, $query);
	{EndDate} = {enddate[0][0]};
}

A nice solution too. Personally I wouldn’t use this solution because of the database dependency, but if you don’t mind than it’s not a problem.