Date format with SSP class

Date format with SSP class

bbrindzabbrindza 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

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    I think you need $d where you have $date - that is, you're passing $d to the formatter which is formatting $date.

  • bbrindzabbrindza Posts: 316Questions: 73Answers: 1

    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');
    }
    ),

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    What is the value of $d at this point: function( $d, $row )?

  • bbrindzabbrindza Posts: 316Questions: 73Answers: 1

    $d = {"time_log_date":"2020-03-23"}
    $row = {"time_log_date":"2020-03-23"}

  • kthorngrenkthorngren Posts: 21,445Questions: 26Answers: 4,974

    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

  • bbrindzabbrindza Posts: 316Questions: 73Answers: 1

    Thanks for the suggestion Kevin, but that returns false as well.

    {"time_log_date":false,"}

  • rf1234rf1234 Posts: 3,021Questions: 88Answers: 421
    edited March 2020

    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:

    Field::inst( 'yourDateField' )
        ->getFormatter( function($val, $data, $opts) {
            $date = new DateTime($val);
            return $date->format("d/m/Y"); //or any other format you require
        }),  
    
  • rf1234rf1234 Posts: 3,021Questions: 88Answers: 421
    edited March 2020 Answer ✓

    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.

    array( 'db' => 'TIME_LOG_DATE','dt' => 'time_log_date',
       'formatter' => function( $d, $row ) {
          //convert obj to assoc array
          $dateArr = json_decode(json_encode($d), true);
          $date = new DateTime($dateArr["time_log_date"]);
          return $date->format("d/m/Y"); //or any other format you require
        }
    ),
    

    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

    $d = (object)["time_log_date" => "2020-03-23"];
    //convert obj to assoc array
    $dateArr = json_decode(json_encode($d), true);
    $date = new DateTime($dateArr["time_log_date"]);
    echo $date->format("d/m/Y"); //or any other format you require
    

    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:

    array( 'db' => 'TIME_LOG_DATE','dt' => 'time_log_date',
       'formatter' => function( $d, $row ) {
          $date = new DateTime($d["time_log_date"]);
          return $date->format("d/m/Y"); //or any other format you require
        }
    ),
    
  • rf1234rf1234 Posts: 3,021Questions: 88Answers: 421
    edited March 2020

    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 ...

    $d = (object)["time_log_date" => "2020-03-23"];
    $date = new DateTime($d->time_log_date);
    echo $date->format("d/m/Y"); //or any other format you require
    
  • bbrindzabbrindza Posts: 316Questions: 73Answers: 1

    rf123 - you where close and helpful. However I need to use $row not $d and removed the comments for the column name. It work!

    array( 'db' => 'TIME_LOG_DATE','dt' => 'time_log_date',
        'formatter' => function( $d, $row ) {
        $date = new DateTime($row[time_log_date]);
        return $date->format("d/m/Y"); //or any other format you require
        }
        ),
    

    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,

This discussion has been closed.