Format UNIX Timestamp to Human Readable Time Format Using Custom Formatter
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.
Example 1 - Date formatter
<?php
// add your own code here....
$dg->set_col_property('last_update',
array('formatter'=>'###dateFormatter###'), 'unformat'=>'###dateUnformat###'); // 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;
}
function dateUnformat (cellValue,
options, rowdata){...}
</script>
Example 2 - Persian number formatter and unformat
<?php
// Example usage in phpGrid
$phpgrid->set_col_property("colname", array(
"formatter" => "###arabicToPersianNumFormatter###",
"unformat" => "###persianToArabicNumUnformat###"
));
?>
<script>
function arabicToPersianNumFormatter(num) {
const persianDigits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
return num.toString().replace(/[0-9]/g, (d) => persianDigits[d]);
}
function persianToArabicNumUnformat(num) {
const arabicDigits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
return num.toString().replace(/[۰-۹]/g, (d) => arabicDigits['۰۱۲۳۴۵۶۷۸۹'.indexOf(d)]);
}
</script>