mysql - 将日期时间从 MySQL 表转换为 ISO8601,以在 WordPress 中创建 JSON 源以与 FullCalendar 一起使用

标签 mysql json wordpress fullcalendar iso8601

我在 WordPress 中编写了一个操作,用于从表中获取行并将其编码为 JSON 格式,以便我可以将它们与 FullCalendar javascript 事件日历一起使用。

表中的日期字段需要采用 ISO8601 格式。

换句话说,当数据库呈现日期/时间:2017-08-06 10:22:20时,我需要在查询后将其转换为:2017-08-06T10:22:20作为日期字段查询。

我不关心时区偏移。

我的功能:

add_action( 'getmyevents', 'get_my_events' );
function get_my_events( $atts = [], $content = null ) {

    // Use WordPress database functions
    global $wpdb;

    // List of events will be stored in JSON format
    $json = array();

    // Query retrieves list of events
    $mytable = $wpdb->prefix . "my_events";
    $myids = $wpdb->get_results("SELECT * FROM " . $mytable );

    // sending the encoded result to success page
    echo json_encode( $myids, JSON_UNESCAPED_SLASHES );

    // return JSON
    return $json;
}

有人可以给我一种快速、直接的方法来将查询中的日期时间字符串转换为 ISO8601 吗?

最佳答案

也许你可以尝试这样的事情。

虽然我不知道你的专栏名称。取消注释 print_r 以获取列名称。

foreach ($myids as $key => $row) {
    // print_r($row);
    $date_reformatted = strtotime($row->date_col);
    $myids[$key]->date_col = date( 'c', $date_reformatted );
}

关于mysql - 将日期时间从 MySQL 表转换为 ISO8601,以在 WordPress 中创建 JSON 源以与 FullCalendar 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45533017/

相关文章:

python - Pandas 数据框到没有索引的json

javascript - 使用 jquery/javascript 将工作 for 循环调用 JSON 分为两部分

css - 应用于背景图像的背景颜色色调效果

Jquery On Hover 文本覆盖

php - 如何在mysql中使用join命令获取搜索查询

mysql - 为什么 MySQL 连接返回循环集?

json - 嵌套 JSON Feed 返回错误

php - 在页面模板上使用工作分页的自定义 wp_query

mysql - 错误 1064 (42000) : You have an error in your SQL syntax

mysql - MySQL基于2列获取最接近的值