What's new

Welcome to xCrud Community - Data Management and extended PHP CRUD

Join us now to get access to all our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, and so, so much more. It's also quick and totally free, so what are you waiting for?

Issues with datetime field

celticjoe

New member
Joined
Dec 2, 2022
Messages
4
Reaction score
2
Points
3
Location
Italy
Perhaps this has already happened to some of you...
I'm using xCRUD v.1.7.15.
I'm having problems with fields of type datetime. When I create a record the current date and time are correctly recorded in the same field, but when I edit an existing record the time is set to zero.
Since we are in Italy, I set it like this in the xcrud_config.php file:


Code:
// date
     public static $date_first_day = 1; // 0 - Sunday, 1 - Monday etc. Uses in datepicker and search ranges
     public static $date_format = 'dd/mm/yyyy'; // jqueryui date format
     public static $time_format = 'HH:mm:ss'; // jqueryui time format
     public static $php_date_format = 'd/m/Y'; // php date format
     public static $php_time_format = 'H:i:s'; // php time format

To set the value of a record at the current moment I use statements like

Code:
$xcrud->pass_default('data_ticket',date('Y-m-d H:i:s'));

which work correctly, the value is correctly recorded and displayed, but the time is reset to zero when the record is modified later, i.e. every time I update it.

I tried with some callbacks, for example in the functions.php file I added this function:

Code:
function data_ticket($postdata, $primary, $xcrud){
     $postdata->set('data_ticket', date('Y-m-d H:i:s', strtotime($postdata->get('data_ticket'))));
}

which I attempted to invoke before or after the upgrade;
this didn't work, like when fetching the value of type datetime existing in the record only the date was extracted.

Indeed,I also tried to store the existing value before the update in a session variable using these instructions in a function associated with the before_update event:

Code:
$data= $postdata->get('data_ticket');
$_SESSION['data_ticket']=$date;

but when i try to insert this value via another function
associated with the after_update event

Code:
$data=$_SESSION['data_ticket'];
$q="UPDATE ticket SET data='$data' where id=$id";
$db->query($q);

it turns out that the SQL instruction already carries the value of the date only, without the time (which was there before):

Code:
UPDATE ticket SET data='2023-07-20 00:00:00' where id=46

From this I think I can deduce that it is the command

Code:
$data= $postdata->get('data_ticket');

to extract the already truncated data.

Anyone have suggestions?
 

celticjoe

New member
Joined
Dec 2, 2022
Messages
4
Reaction score
2
Points
3
Location
Italy
In case anyone is interested...
I seem to have found now a working turnaround, by inserting a line that explicitly sets the value of the field equal to the value contained in the edit form

Code:
$xcrud->pass_var('data_ticket', '{data_ticket}', 'edit');

sounds a bit silly, but it works....

...although in my opinion it remains a little bug in the code, but I couldn't find where it is.
 
Top Bottom