Yii PHP 日期过滤
YII 按照日期过滤不起作用,怎么回事?模型
<?php/** * This is the model class for table "user". * * The followings are the available columns in table 'user': * @property integer $id * @property string $uname */class Timefliter extends CActiveRecord{ /** * Returns the static model of the specified AR class. * @param string $className active record class name. * @return User the static model class */ public static function model($className=__CLASS__) { return parent::model($className); } /** * @return string the associated database table name */ public function tableName() { return 't_time'; } public $starttime; public $endtime; /** * @return array validation rules for model attributes. */ public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('id', 'safe'), //array('id', 'numerical', 'integerOnly'=>true), array('time', 'safe'), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, time', 'safe', 'on'=>'search'), ); } /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( ); } /** * @return array customized attribute labels (name=>label) */ public function attributeLabels() { return array( 'id'=> '编号', 'time'=> '时间', ); } /** * Retrieves a list of models based on the current search/filter conditions. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. */ public function search() { // Warning: Please modify the following code to remove attributes that // should not be searched. $criteria=new CDbCriteria; $criteria->compare('id',$this->id); $criteria->compare('time','>='.$this->starttime); $criteria->compare('time','<='.$this->endtime); //是否开启模糊搜索,在后面true中实现 return new CActiveDataProvider($this, array( 'criteria'=>$criteria, //'pagination'=>array('pagesize'=>$pagesize?pagesize:$page), //'pagination'=>array('pagesize'=>$page), )); }}
视图
<form method="post" ><!--时间查询-->开始时间<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array( 'language'=>'zh_cn', 'name'=>'starttime', 'value'=>$query['start'] ? $query['start'] : Date('Y-m-d'), 'options'=>array( 'showAnim'=>'fold', 'showOn'=>'both', 'buttonImage'=>Yii::app()->request->baseUrl.'/images/calendar.gif', //'maxDate'=>'new Date()', 'buttonImageOnly'=>true, 'dateFormat'=>'yy-mm-dd', ), 'htmlOptions'=>array( 'style'=>'height:18px', 'maxlength'=>8, ), )); //echo $value;exit; //http://blog.csdn.net/dreamzml/article/details/8227646?> 结束时间<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array( 'language'=>'zh_cn', 'name'=>'endtime', 'value'=>$query['end'] ? $query['end'] : Date('Y-m-d'), 'options'=>array( 'showAnim'=>'fold', 'showOn'=>'both', 'buttonImage'=>Yii::app()->request->baseUrl.'/images/calendar.gif', //'maxDate'=>'new Date()', 'buttonImageOnly'=>true, 'dateFormat'=>'yy-mm-dd', ), 'htmlOptions'=>array( 'style'=>'height:18px', 'maxlength'=>8, ), )); ?> <!--$criteria->compare('create_time','<='.$this->endtime),//创建早间小于等于指定时间 这个名字name就是widget里面的name的数据库字段,将它放到搜索页面中指定前端名字,和后端数据库的字段在模型module里面指定compare的条件即可实现时间搜索--> <input type="submit" name="search" value="搜索"/></form>
控制器
public function actionTimefliter() { $model=new Timefliter(); $this->render('timefliter',array('model'=>$model,)); }
回复讨论(解决方案)
提交之后又没把获取的日期值赋值给模型,那在search方法中怎么会过滤
怎么复制给模型
可以用另外一种解决方案,自己新建一个dataprovinder 然后sql语句查询时间之间的记录
$model->startTime=$_POST['starttime']; //传值给模型$model->endTime=$_POST['endtime'];
谢谢楼上两位,都搞定了,两种方法都是可行的,不过第二种比较慢

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
