假设我在 MySQL 中有一个表,其中包含 DateTime
类型的字段。我们将该字段称为 fa
。
现在假设我在 Yii 1 (1.1.16) 中有以下代码:
$model = MyTable->createNewModel(); // <-- pseudo code
$model->fa = new DateTime("now");
$model->save();
$model = MyTable->getByPk(1); // <-- pseudo code
$model->fa; // <-- question 1
$model->fa = $model->fa->modify("+1 month"); // <-- question 2
$model->save();
我有以下问题:
问题1:
变量 $model->fa
是什么类型(位于带有“问题 1”注释的行)?是字符串吗?它是 PHP 的 DateTime
类型吗?或者它是 DateTime
的某种 Yii 表示形式?
问题2:
如果我上一个问题的答案是“PHP的DateTime
类型”,那么带有“问题2”注释的行是完全有效的,并且应该没有任何问题。但是$model->fa
的类型不是PHP的DateTime
类型吗?如何向 $model->fa
添加 1 个月?
另请注意,我知道我可以使用 format
进行修改,并制定一个丑陋的解决方案,其中涉及解析我自己的日期/时间格式表示,但我想避免如果可能的话。
最佳答案
第一季度: 是的,它必须是 DateTime 类型。
第二季度: 您可以使用此函数向 dateTime 变量添加一个月。
function addMonth($iDate,$num = 1)
{
$date = $iDate->format('Y-n-j');
list($y, $m, $d) = explode('-', $date);
$m += $num;
while ($m > 12)
{
$m -= 12;
$y++;
}
$last_day = date('t', strtotime("$y-$m-1"));
if ($d > $last_day)
{
$d = $last_day;
}
$fDate = new DateTime();
return $fDate->setDate($y, $m, $d);
}
关于php - DateTime 数据类型、MySQL 和 Yii,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28765201/