0

我正在 yii 中创建项目。我的投票表的字段为--pollId -pollQustion -isActive -publishDate

我想检查民意调查的天气发布日期不大于当前日期。我正在实施 -

$CurrrentDate=new CDbExpression('NOW()');
if($record->publishDate < $CurrrentDate))
{
  some code......
}

但它没有正确执行。即使发布日期大于当前日期,代码也会执行。那么如何在 yii 框架中进行这种比较。请帮我

4

1 回答 1

0

PHP 没有日期格式。当您比较 $CurrentDate 和 $record->publishDate 时,您比较的是字符串。好吧,为了正确比较,您需要将其转换为 Unix 的时间戳

// here you set right date/time pattern 
// You need to explain CDateTimeParser for your pattern
// just easy use `strtotime` if you have 2012-12-12 12:12:12
$publishTimestamp = strtotime($record->publishDate);
// $publishTimestamp = CDateTimeParser::parse($record->publishDate, 'yyyy-MM-dd hh:mm:ss'); 
// if you need to use Hours and minutes just easy use `time()`
$currentDateTimestamp = time();
// $currentDateTimestamp = strtotime(date('Y-m-d H:i:s'));
// now we can compare
if ($publishTimestamp  < $currentDateTimestamp) {
   // do some 
}
于 2012-12-12T06:45:57.790 回答