Skip to content

Format UNIX Timestamp to Human Readable Time Format Using Custom Formatter

For UNIX timestamp, the set_col_edittype will not work due to the data format. A workaround is to use custom formatter javascript with a little bit "hack":

The sample code below, last_update is the the database table field in UNIX timestamp type. dateFormatter is the javascript function to convert UNIX timestamp to readable date time. Must surround the javascript function name with ### to be later escaped by phpGrid internal code.

<?php
// add your own code here....
$dg->set_col_property('last_update', array('formatter'=>'###dateFormatter###')); // must include ###
// more code here....
 ?>

<script>
    // cellValue:string - current row "last_update"  value
    // options:object - column "last_update" object with colModel, rowId, rowdata (useful!)
    // rowdata:JSON - row data
    function dateFormatter (cellValue, options, rowdata)
    {
        if (cellValue==0){
            return 'N/A';
        }
        var time = new Date();
        time.setTime (cellValue * 1000);
        var day = time.getDate();
        var month = time.getMonth()+1;
        var year = time.getFullYear();
        var hours = time.getMinutes();
        var minutes = time.getMinutes();

        var niceDate = year+'/'+month+'/'+day;
        return niceDate;
    }
</script>


Feedback and Knowledge Base