Adding Hours to Current DateTime Field before inserting record

Hi All,
I have created a form and it has a field with DateTime type which records the inserted date and time of a new record. To do this I selected the option ‘DateTime of inclusion’ under Database value. (please see attached image)

This is working great but for some records I need to add different hours. For one table I need to add 2 hours and for another table I need to add 1.5 hours. How can I implement something like that in my application.
Basically I want to save the DateTime field with value = Current DateTime + 2 hours

SELECT NOW() + INTERVAL 90 MINUTE; (in phpmyadmin I can retrieve that using this query)

Thank you,
Cheers,
Gayan

Datetime.jpg

Re: Adding Hours to Current DateTime Field before inserting record

Hi,
I guess it depends on a condition what time to save.
I wouldn’t use “DateTime of Inclusion”. I suggest to set the “Initial Value” to “System Date” instead.
Then you can code an IF-statement in the “onBeforeInsert” event of the form.

IF(condition)
{
{datetime_field}=date(“Y-m-d H:i:s”,strtotime(’+90 Minutes’));
}
ELSE
{
{datetime_field}=date(“Y-m-d H:i:s”,strtotime(’+60 Minutes’));
}

That should do it.

jsb

Re: Adding Hours to Current DateTime Field before inserting record

Hi JSB,
Thank you very much for your fast response. I have managed to get what I want by following your instructions.

I didn’t need the ‘IF’ statement as there is no condition for that. One Form is always 1.5 hours extra and wanted add 2 hours extra on another separate Form. Sorry I didn’t make my question clear but it was a good tip for later use.

Only problem I faced was the DateTime returned was 13 hours back. To get the Current Date Time I had to do this.

{DateTime}=date(“Y-m-d H:i:s”,strtotime(’+13 Hours’));

That returned the current time. Is it something to do with the incorrect timezone or something?

But anyway just to get the correct time result I want I have slightly modified your code as per below and added that in ‘OnBeforeInsert’ event.
{DateTime}=date(“Y-m-d H:i:s”,strtotime(’+750 Minutes’));

Thanks again for your help on this. :slight_smile:
Cheers,
Gayan

Re: Adding Hours to Current DateTime Field before inserting record

Yes it has to do with the timezone your system uses. You can check it with:

echo date_default_timezone_get();

If you have access to the php.ini find the line "date.timezone = " and set it to the correct (your) timezone:

i.e. date.timezone=“Europe/Berlin”

That should fix it.

jsb

I do not understand. If I change Zone in PHP.INI , even it it works and reflects correct time it is only on my PC,…what about other user ?
How can I get the timezone and incorporate it into the curent datetime calculation, so I always get the correct DateTime() that also reflects the timezone ?

I tried to use this

{LastUpdate}=date("m-d-y H:i:s",strtotime('+420 Minutes'));

but I get a date in year 1959, the time is also incorrect!

Arthur