Cell format: Display timestamp value as friendly date

This tutorial is for developers.

For example, our posts have one meta field with a timestamp and the spreadsheet shows the timestamp like this:

cell-format-display-timestamp-value-as-friendly-date

We can use the following code to display the values as friendly dates, so we can view, edit, and save the column using friendly dates:

add_filter('vg_sheet_editor/provider/post/update_item_meta', 'wpse_expiration_filter_cell_data_for_saving', 10, 3);
add_filter('vg_sheet_editor/provider/post/get_item_meta', 'wpse_expiration_filter_cell_data_for_readings', 10, 5);
function wpse_expiration_filter_cell_data_for_readings($value, $id, $key, $single, $context) {
if ($context !== 'read' || $key !== '_expiration-date' || ! $value) {
return $value;
}
$value = get_date_from_gmt(gmdate('Y-m-d H:i:s', $value ));
return $value;
}
function wpse_expiration_filter_cell_data_for_saving($new_value, $id, $key) {
global $wpdb;
if ($key !== '_expiration-date' || ! $new_value) {
return $new_value;
}
$new_value = get_gmt_from_date($new_value, 'U');
return $new_value;
}

Result:

cell-format-display-timestamp-value-as-friendly-date

 

How to edit the date using a calendar

If you want something like this, you need the following code:

cell-format-display-timestamp-value-as-friendly-date

add_action('vg_sheet_editor/editor/before_init', 'wpse_modify_expiration_column', 999);
function wpse_modify_expiration_column($editor) {		
$post_type = 'post';
$meta_key = '_expiration-date';
$editor->args['columns']->register_item($meta_key, $post_type, array(
'formatted' => array('data' => $meta_key, 'type' => 'date', 'dateFormat' => 'YYYY-MM-DD', 'correctFormat' => true, 'datePickerConfig' => array('firstDay' => 0, 'showWeekNumber' => true, 'numberOfMonths' => 1))
), true);
}

Where do I add this code?

Add it in the functions.php in your theme or your child theme folder, or you can use the “code snippets” plugin to load it in wp-admin.

Do you need help?

You can receive instant help in the live chat during business hours, or you can contact us and we will help you via email.