Date format with SSP class
Date format with SSP class
bbrindza
Posts: 316Questions: 73Answers: 1
I need to format a date from a DB2 date type column in my SSP script.
If I use the formatter function the return is null.
{"draw":0,"recordsTotal":1,"recordsFiltered":1,"data":[{"time_log_date":false}]}
array( 'db' => 'TIME_LOG_DATE','dt' => 'time_log_date',
'formatter' => function( $d, $row ) {
return date_format($date, 'l F d, Y');
}
),
However if I just set the array with the column without the formatter is returns the data in the column .
{"draw":0,"recordsTotal":1,"recordsFiltered":1,"data":[{"time_log_date":"2020-03-20"}]}
array( 'db' => 'TIME_LOG_DATE','dt' => 'time_log_date'),
What am I missing.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
I think you need $d where you have $date - that is, you're passing $d to the formatter which is formatting $date.
Made the change and still returns false.
array( 'db' => 'TIME_LOG_DATE','dt' => 'time_log_date',
'formatter' => function( $d, $row ) {
return date_format($d, 'l F d, Y');
}
),
What is the value of $d at this point: function( $d, $row )?
$d = {"time_log_date":"2020-03-23"}
$row = {"time_log_date":"2020-03-23"}
I'm not familiar with PHP but do you need to do something along the lines of
return date_format($d["time_log_date"], 'l F d, Y');
Kevin
Thanks for the suggestion Kevin, but that returns false as well.
{"time_log_date":false,"}
Hmmm ... I am using Data Tables not with the SSP class but with Editor and PHP and MySQL.
Here is something on getFormatters for dates that might be helpful:
https://datatables.net/forums/discussion/comment/146135/#Comment_146135
Assuming your date comes from an SQL database in the usual format:
"2020-03-24 00:00:00" or as "2020-03-24".
And you just want one front-end date format regardless of the user language. Then it is really easy just using plain PHP, no built-in formatters:
if $d = {"time_log_date":"2020-03-23"}
which means it is an object ?! (I am not so sure ...)
this should work unless it is not an object OR there is another bug in there.
I just tried this in PHP sandbox and it worked. Please note: I had to create the object with PHP which is different syntax then what you present. So in the code below $d is definitely a PHP object. And this object is being converted to an associative array
Result: 23/03/2020
This is what you need to check whether your variable really is an object:
https://www.php.net/manual/en/function.is-object.php
So if $d = ["time_log_date" => "2020-03-23"] which means it is an associative array you can simply skip the conversion:
forgot to mention that this also works if it is an object ... sorry. When working with Editor I am so used to assoc arrays that I didn't even think about this simple way ...
rf123 - you where close and helpful. However I need to use $row not $d and removed the comments for the column name. It work!
Using $d in DateTime function threw this error on my PHP server.
Uncaught Exception: DateTime::__construct(): Failed to parse time string (2) at position 0 (2): Unexpected character in /www/zendphp7/htdocs/TimeLog/ssp_TimeLogTable.php:33 Stack trace: #0 /www/zendphp7/htdocs/TimeLog/ssp_TimeLogTable.php(33): DateTime->__construct('2') #1 /www/zendphp7/htdocs/nwfFunctions/ssp.class.php(35): {closure}('2020-03-24', Array) #2 /www/zendphp7/htdocs/nwfFunctions/ssp.class.php(269): SSP::data_output(Array, Array) #3 /www/zendphp7/htdocs/TimeLog/ssp_TimeLogTable.php(70): SSP::simple(Array, Resource id #1, NULL, 'NWFF.TIME_LOG', 'EMPLOYEE_NUMBER', Array, '') #4 {main} thrown
**THANK YOU ** so much for your insight,