We help IT Professionals succeed at work.

Clearing PHP calculated field

CynSzcz asked
Working with a PHP calculated data field.  It relies on an "anchor" date data field elsewhere in the system, it takes the date and adds 30 days to it (or 15, or minus 45, whatever).  All works fine until someone clears the anchor date, then my calculated date field reverts to Jan 1, 1970 and then adds (or subtracts) the days added/subtracted in the calculated field.  

The date calculated data field is:

$date = '[[pri_mtgs_and_key_dates_complaint_filed_date]]';
$date = date('M d, Y', strtotime('+15 days', strtotime($date)));
echo $date;

Is there a way to insert some code in the calculated field to say, "if the date field this calculated field uses  is cleared, clear the result of the calculated field also?
Watch Question


The question is a little unclear when it comes to where the calculated field is being shown. Can you tell us if any of these situations match what you're asking:

OPTION 1. The "anchor" date field is stored in the database. A user comes to a PHP page, and the new date field is calculated when the page loads, BUT it is not permanently stored anywhere (e.g. it is not saved back to the database) .

OPTION 2. The "anchor" date field is stored in the database, and at the time that it is stored, there is code that is calculating this new field and saving the calculation into the database. Then when someone visits the page, it is displaying the value that was saved in the database.

OPTION 3. Nothing is permanently stored (no database). The "anchor" date field is defined on a web form and then on the next screen, the new date field is calculated on-the-fly and shown to the user.

Can you also clarify what you mean when you talk about the anchor date being cleared. How is it cleared and who clears it and when?




Ok, I can confirm that Option 3 is out!  My anchor field is a date field and saved to the database (can use it in merge docs, etc.). I create a calculated field with a separate "merge field name" and in the "calculation code" box I put the code I copied in my original question above which is using the anchor data info and SAVE the calculated field.  Then, I go back to where I created the anchor date in a specific data collection area, create a new merge field that I define as "Custom HTML (for display purpose)" type, give it a name, and in the description box (for this new field) I place the [[name of the calculated field]].

When a user goes into a specific matter (it's a legal software) and views the data collections they can click on "edit" and change the dates, text, etc.  If they edit a date field (anchor date field) and click "clear date" that is when the calculated field relying on this date (perhaps in another data collection) goes to 1/1/1970 plus however many days is in the calculation.

There are times when a user may go into the wrong matter and start filling in dates and then have to clear them.  But then the other data collections are going have calculated fields for 1970 and the lawyers are going to loose their "stuff."

Does any of that answer your question?   I am thinking the answer is Option 2


So this sounds like you're a power user inside of some existing application that you did not build yourself. The process you described sounds like you're defining some code, but the application is responsible for actually running it and presenting the field.

If that's accurate, then you'd have to go into the application code to figure out how it's executing your code, and whether or not it is caching the values somewhere. Within that parent application, you'd have to adjust code so that it performs some kind of action whenever the original / anchor date field is cleared (this technique is typically referred to as a "hook" or a "callback", depending on the situation), and then you'd have to figure out how to make that action clear the cached value.

Unfortunately, there's no way anybody can answer this without knowing the insides of that application. So either you'll have to figure this out on your own, or have the application owner figure it out. Worst case, you can hire someone (some experts on here are available for consulting work) to figure it out. 



You are spot on!!!!  And correct, I will have to ask if anyone in development thinks this is possible.  I do really appreciate your time and suggestion.  

Most Valuable Expert 2018
Distinguished Expert 2019

If you have control over the calculated field code, then it should be straight forward enough.

The problem you seem to be having is because the value of [[pri_mtgs_and_key_dates_complaint_filed_date]] is likely to be null, false or 0 after it's been cleared. You then try and pass that into strtotime, which in turn will return false. You then pass that in to strtotime and try to add 15 days to it. When false is passed into strtotime, it will default to the epoch - 01 Jan 1970, and then add (or subtract) your days from that.

You should be able to avoid that by first checking the result of calling strtotime on your anchor date. If it's false, then return something other than the calculated field. What you return will depend on your application needs, but try something like this:

if ($anchorDate = strtotime('[[pri_mtgs_and_key_dates_complaint_filed_date]]')) {
    echo date('M d, Y', strtotime('+15 days', $anchorDate));
} else {
    echo "Could not calculate the date!";

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2019

echo $date ?  date('M d, Y', strtotime('+15 days', strtotime($date)));  : '';

Open in new window


I must be tired. I completely overlooked something in your question:

...then my calculated date field reverts to Jan 1, 1970...

For whatever reason, I thought you were still seeing the old calculated value, as if it were cached. If you're seeing a new value based on 1970, then it's not cached. Given that, disregard what I said and follow either Chris or Julian's suggestions.

Arijit SConsultant

Isn't it about ensuring that even if someone clears the date, you put a default date - to ensure your calculations don't break?
If so - what should be the logic for clearing the date? To set the current date as anchor?

In that case:
$date = '[[pri_mtgs_and_key_dates_complaint_filed_date]]';
$date = isset($date)? $date:date("M d, Y");



Thank you all for your help!  I learned a lot and Chris' suggestion does work.  I can place a date in the anchor field and if cleared (entered by mistake or something), the field that is dependent upon that date will display the text "Anchor Date Empty."  

I wish I could pay you all or something.