This topic is locked

Date calculation

3/14/2023 7:26:42 AM
PHPRunner General questions
bluestrikealpha author

I have tried to set up an event to calculate the date 12 months later from the field createdate to save it in next inspection:
$values["next inspection"] = date("Y-m-d",strtotime($values["createdate"])+(60<em>60</em>24<em>30</em>12));
but I cant get it to work.
Can anybody help me here?

jadachDevClub member 3/14/2023

Sorry, this is PHP not C#
Try this before record added
DateTime dt1 = DateTime.Parse(values["DateEntered"].ToString()); values["NextDate"] = dt1.AddMonths(12).ToString();

admin 3/14/2023

It will help if you explain what exactly doesn't work right now. It doesn't insert any value into "next inspection" field or inserts an incorrect value?

bluestrikealpha author 3/15/2023

No, It does not fill in any value in Next inspection

bluestrikealpha author 3/15/2023

That gives : syntax error, unexpected 'dt1' (T_STRING) in line 1

sfuente 3/15/2023

// Fecha actual
$fecha_actual = date('Y-m-d');
// Sumar un año a la fecha actual
$nueva_fecha = date('Y-m-d', strtotime('+1 year', strtotime($fecha_actual)));
// Imprimir la nueva fecha
echo $nueva_fecha;

admin 3/15/2023

You have to start from the very beginning and provide all the relevant info.
<ol> What event do you use? Do you expect this value to appear on the page on right in your database?

bluestrikealpha author 3/15/2023

To admin:
I made one field "Createdate" and another which is"Next inspection". When I fill in the date of today (as default) in "Createdate" I want to see on the same page in the field "Next inspection" the same date but 12 months later.
Does that make sense to you?

admin 3/15/2023

You didn't answer the question number 1.

bluestrikealpha author 3/16/2023

Sorry about that ; Question no 1: Before record added and before record edited, seems odd but I have taken that from an example given here.

admin 3/16/2023

Thank you.
BeforeEdit/BeforeAdd events are executed after user clicks Save button. This means that if you need something to be recalculated on the screen, in realtime, this event will not work for you.
To achive what you looking for you need to use Field Events and Javascript code:
Example 2 shows an example of calculation and filling another field.

bluestrikealpha author 3/17/2023

But then I need a Java string and no PHP to calculate 12 months more. Who has done that already ?

admin 3/17/2023

Google "javascript add year to date" and it shows tons of examples.
I like this one for instance: