gpt4 book ai didi

php - 使用 get_post_meta 将 unix 时间戳改回 mysql 日期时间

转载 作者:行者123 更新时间:2023-11-29 02:38:39 25 4
gpt4 key购买 nike

我有一个脚本可以将我的 get_post_meta 从数据库转换为 unix,将变量与另一个变量进行比较,如果它有不同的 unix 时间,它将把它写回数据库。

$wp_modified = strtotime(get_post_meta($office->ID, '_last_modified', true));
Outputs:
2018-10-30 00:02:00 to 1571304301

然后在下面,我需要在使用 update_post_meta 写入之前将 _last_modified post meta 转回 2018-10-30 00:02:00 格式。

if ($time_changed) {
update_post_meta($office->ID, '_last_modified', $record->updated_at);
}

在执行 update_post_meta($office->ID, '_last_modified', $record->updated_at) 之前,我如何才能将它从 1571304301 改回 2018-10-30 00:02:00; 调用?

我会做这样的事情吗?

    if ($time_changed) {
$test = get_post_meta($office->ID, '_last_modified', true);
$test1 = date('Y-m-d H:i:s', $test);
update_post_meta($office->ID, '_last_modified', $test1);
}

最佳答案

对于 MySQL,你有一些函数可以帮助你处理 UNIX 时间戳,例如:

SELECT from_unixtime(1540872120), unix_timestamp("2018-10-30 00:02:00")

有了它,您可以直接使用 MySQL 结果而无需在 PHP 中进行转换。

对于 PHP:

我认为您拥有的代码:date('Y-m-d H:i:s', 1540872120) 可以解决问题。

关于php - 使用 get_post_meta 将 unix 时间戳改回 mysql 日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58433436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com