gpt4 book ai didi

php - 在 $model->find Yii 中使用 UTC_TIMESTAMP()

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

我想知道如何使用 Yii 中的 CActiveRecord 实现此目的:

SELECT name, surname, UTC_TIMESTAMP(last_seen) FROM users WHERE id = '12345'

我已经使用这种方法编写了大部分代码:$model->findAll(...),但我无法将我最喜欢的 MySQL 时间戳函数合并到查询中。

我需要它能够在 PHP 中使用 date_default_timezone_set() 设置时区。

我的时间戳以 TIMESTAMP 格式存储在 mysql 表中。

编辑

我并没有尝试将时区从 PHP 传递给 MySQL。我使用 CURRENT_TIMESTAMP 作为默认值存储我的时间戳。我唯一想使用 date_default_timezone_set() 的时候是我想在用户的时区中显示这个日期。

抱歉,如果我不够清楚,感谢您的输入:-)

最佳答案

在您最近的评论之后,是的,有一种方法可以通过 date time functions .如果您检查 documentation of findAll();可以接受 2 个参数,一个用于条件或条件,第二个用于绑定(bind)到生成的 sql 的参数。

我们可以使用第一个参数来传递条件并使用 CDbCriteria's select property , 哪个

refers to the select clause of an sql statement

因此我们可以这样做:

$dbCriteria=new CDbCriteria;
$dbCriteria->select='name, surname, UTC_TIMESTAMP() as some_alias'; // we need the alias so that the timestamp is easily accessible
// you can also pass * if you need to access all the values of the table, alongwith the timestamp, like '*, UTC_TIMESTAMP() as some_alias'
$model->findAll($dbCriteria);

但是当然要使用 some_alias 你需要在你的模型中声明它:

public $some_alias; // this will be automatically be populated by the alias, when you query the db, in findAll

关于php - 在 $model->find Yii 中使用 UTC_TIMESTAMP(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11599349/

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