【问题标题】:Output format datetime in yiiyii中的输出格式日期时间
【发布时间】:2013-06-21 14:13:48
【问题描述】:

我是 yii 框架的新手。
以 2 个字段的形式创建了一个表单(不使用内置小部件)。
time_start , time_end - 输入数据库 DATETIME。
数据库中的所有数据都已正确记录,但如果您输入字母而不是字母,则会写入零。
我尝试在模型中添加一条规则:

array ('time_start, time_end', 'type', 'type' => 'datetime', 'datetimeFormat' => 'Y-m-d H:i:s'),

似乎格式与存储在数据库中的格式相同,但没有任何反应。 在控制器的一个方法safe()之后,写道:

print_r ($ model-> getErrors ());

例如引入字段: 2006-12-26 15:00:30 获取数组:

Array ([time_start] => Array ([0] => Start must be datetime.) [Time_end] => Array ([0] => End must be datetime.))

等尝试了不同的格式,但错误都是一样的。 告诉我,我该怎么做才能使您只能以格式输入 (例如数据库),仅此而已。 提前致谢。


*更新 *
在“views/site/index”中:

  <?php $this->widget('zii.widgets.CListView', array(
        'dataProvider'=>$modeltask->search(),
        'itemView'=>'_viewtask',
    )); ?>

查看任务

<div class="view">
    <b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
    <?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>
    <br />
    <b><?php echo CHtml::encode($data->getAttributeLabel('author_id')); ?>:</b>
    <?php echo CHtml::encode($data->author_id); ?>
    <br />
    <b><?php echo CHtml::encode($data->getAttributeLabel('performer_id')); ?>:</b>
    <?php echo CHtml::encode($data->performer_id); ?>
    <br />
    <b><?php echo CHtml::encode($data->getAttributeLabel('parent_task_id')); ?>:</b>
    <?php echo CHtml::encode($data->parent_task_id); ?>
    <br />
    <b><?php echo CHtml::encode($data->getAttributeLabel('project_id')); ?>:</b>
    <?php echo CHtml::encode($data->project_id); ?>
    <br />
    <b><?php echo CHtml::encode($data->getAttributeLabel('hours_plan')); ?>:</b>
    <?php echo CHtml::encode($data->hours_plan); ?>
    <br />
    <b><?php echo CHtml::encode($data->getAttributeLabel('title')); ?>:</b>
    <?php echo CHtml::encode($data->title); ?>
    <br />
<form action="http://testik.test/index.php?r=site/savec" method="post">
<input type="hidden" value="<?php echo $data->id; ?>" name="id_task">
<input type="hidden" value="<?php echo Yii::app()->user->id ?>" name="id_user">
<input type="text" placeholder="Дата начала" name="time_start">
<input type="text" placeholder="Дата конца" name="time_end">
<input type="submit" value="Challenge accepted">
</form>
</div>

SiteController,方法 actionIndexactionSaveC :

public function actionIndex()
    {       
    $modeltask=new Task('search');
    $modeltask->unsetAttributes();
    if(isset($_GET['Task']))
        $modeltask->attributes=$_GET['Task'];   
        $this->render('index', array(
            'modeltask'=>$modeltask,            
        ));
    }
    public function actionSaveC()
    {

$model = new Calendar;
$model->unsetAttributes();
if(!empty($_POST))
{
  $model->attributes = $_POST;
  $model->save();
print_r($model->getErrors());

  //$this->redirect(array('index'));
}

模型:添加了一行,其他没有任何变化
忘了说所有都创建了 CRUD

【问题讨论】:

  • 你的意思是'type' 然后'type' =&gt; 'datetime'
  • 您必须向我们展示您的控制器、表单模型和视图才能让我们回答这个问题,但如果您不使用表单小部件,您可能会失去一些必须编写的功能自己。
  • @Pitchinnate ,我在互联网上寻找需要在模型中添加的规则。只记录所需的格式
  • @jmarkmurphy,添加代码

标签: mysql date datetime yii


【解决方案1】:

您的验证规则定义错误。如果您只想从用户那里接受格式正确的日期,则规则应为

array ('time_start, time_end', 'date', 'format' => 'yyyy-MM-dd HH:mm:ss'),

这会设置一个带有正确日期formatCDateValidator,您必须指定它不是使用PHP 的DateTime 约定,而是使用CDateTimeParser 的约定。

【讨论】:

  • 你能详细解释一下吗?
  • @S1ash:具体解释一下?
  • 如果您不忙,能否在我的示例中解释一下如何操作?
  • 你在你的问题中给出了一个规则。用这个答案中的规则替换它。
  • 可以为模型中的给定属性创建两个或多个规则,但所有规则都必须通过验证才能通过。如果您想要一个“任何”验证器,您必须自己创建它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
相关资源
最近更新 更多