Hook storing bad value for date field

Hello all,

We are trying to set a date field from a hook but the stored value does not have nothing to do with the intended value.

Here is the line we need (the date is force, but the given value is the same.


$comDate = date("d/m/Y", strtotime("2018-04-17"));
 $bean->x_date_field_c = $comDate;

The store value is “05/04/2019”

We do not set this custom value in any other location then this hook. Can anyone help us?

Try changing this part to use dots instead of dashes

strtotime("2018.04.17")

Hello,

if we change the string the stored value changes to ‘01/01/1970’.

I don’t know, then, sorry.

I think there are some higher-level objects in SuiteCRM to handle dates and timezones, maybe you could try those

Like these ones here:
https://stackoverflow.com/questions/17445718/programmatically-set-start-date-time-in-calls-edit-view

TimeDate object
asUser function

maybe if you read up on those you might find clues, or some usable code. If not, you’re better off asking in StackOverflow, this is a PHP issue… Good luck

Hello,

I have already passed through that page, but no success. I know Sugar has it’s own DateTime class. Can this be the cause of the problem?

Hi,

If you are storing a date in a logic hook then you will need to set it in the database format of ‘y-m-d’ UTC. No other format will be excepted.
This is because the data in a before save logic will be ‘ready’ to just be inserted and the conversion process has already been done from user perference into UTC.

So really all you need to do is:

$comDate = date("Y-m-d", strtotime("2018-04-17")); // Which is actually redundant....
 $bean->x_date_field_c = $comDate;

Now if you were pulling in linked beans and trying to convert them into a suitable database format THAT is a different task…

2 Likes

Hello,

You are right samus, thank you!