php / Codeigniter--如何比较只有日期排除时间 [英] php/Codeigniter-- How to compare only date by excluding time

查看:155
本文介绍了php / Codeigniter--如何比较只有日期排除时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的数据库表中有一个字段 creater_date 为日期时间,并且以的形式存储值<2013-09-13 02:12 :44

i have a field in my db table as creater_date as datetime and having values stored in the form of 2013-09-13 02:12:44

现在我不得不比较今天 - 没有时间与 creater_date 表字段。

Now i have to compare today-date(no time) with creater_date table field .

我尝试下面的代码但是显示错误:

I tried this below code but it is showing error :

 function check_existing_User_weightStatus($u_id)
 {
    $this->load->database();
    $this->load->helper('date');  
    $today = date('Y-m-d');
    $array = array('creater_id' => $u_id,DATE('created_date') => $today);
    $this->db->where($array); 
    $query = $this->db->get('user_weight');
    if ($query->num_rows() > 0) {
        return true;
    } else {
        return false;
    } 
 }

获取错误 / p>

Getting error :

 A Database Error Occurred

 Error Number: 1064

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '13 Sep 2013 02:48:27 +0530Asia/Calcuttaam30Asia/Calcutta13_13am30Asia/Calcutta '' at line 4

 SELECT * FROM (`user_weight`) WHERE `creater_id` = '3235' AND `2013-09-13T02:48:27+05:30Fri,` 13 Sep 2013 02:48:27 +0530Asia/Calcuttaam30Asia/Calcutta13_13am30Asia/Calcutta '2013-09-13'

 Filename: D:\xampp\htdocs\webapp\system\database\DB_driver.php

 Line Number: 330

我不想与时间比较,我只想comapare与今天的日期。字段 created_date (例如,2013-09-13 02:12:44)具有 datetime 数据类型值。但是我只想比较日期

i do not want to compare with the time , i only want to comapare with the today date. The field created_date(eg. 2013-09-13 02:12:44) is having datetime datatypes with some time values. But i want to compare only with the date

推荐答案

问题是你的代码生成错误的SQL语法,就像错误显示的那样。

The problem is that your code generates a faulty SQL syntax, like the error shows.

我不是codeIgniter的专家,但这里是如何直接进行普通查询,这可能是你想做的:

I'm not an expert with codeIgniter, but here's how to do a normal query directly, that's probably what you want to do:

function check_existing_User_weightStatus($u_id)
 {
    $today = date('Y-m-d');
    $this->load->database();
    $query = $this->db->query("SELECT * FROM `user_weight` WHERE `creater_id` = '$u_id' AND DATE(`created_date`) = '$today'");

    if ($query->num_rows() > 0) {
        return true;
    } else {
        return false;
    } 
 }

您的代码中的错误发生在此行

The error in your code is occurring at this line

$array = array('creater_id' => $u_id,DATE('created_date') => $today);

我很确定这不是如何执行where子句,所以你可以查找codeIgniter docs!找到正确的方法来做到这一点! (你不是告诉where子句使用 AND 等操作符)

I'm pretty sure this is not how the where clause will be done, so you might lookup the codeIgniter docs ! to find the right way to do that ! (You're not telling the where clause to use AND, OR, etc.. operators)

这篇关于php / Codeigniter--如何比较只有日期排除时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆