How to Get Only Date from Datetime in Codeigniter

How to get only date from datetime in CodeIgniter?

try with $this->db->like();

so it will be

$query=$this->db->like('dates', array('dates' => date('Y-m-d')));

the % will be added automatically

also you have datas as key, and should be dates based on database structure

Extract only date from datetime format stored in database

get the datetime variable from the database and then use

.ToShortDateString(), or .ToString("dd/MM/yyyy");

php/Codeigniter-- How to compare only date by excluding time

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

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');
$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);

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)

CodeIgniter Date or DateTime

When your field in MySQL is Date-time it aspects proper format before saving in database.

So convert string to date time format in php using.

date("Y-m-d H:i:s", strtotime($string)); 

Or you can do the same in Datepicker

$(function() {
dateFormat: 'yy-dd-mm',
onSelect: function(datetext){
var d = new Date(); // for now
var h = d.getHours();
h = (h < 10) ? ("0" + h) : h ;

var m = d.getMinutes();
m = (m < 10) ? ("0" + m) : m ;

var s = d.getSeconds();
s = (s < 10) ? ("0" + s) : s ;

datetext = datetext + " " + h + ":" + m + ":" + s;

Fiddle for datepicker

Note if you want date only in MySQL field the omit hour min sec part of conversion

How to compare only date and year in php codeigniter

Use like this :

$this->db->where('EXTRACT(YEAR_MONTH FROM P.payscale_date)',date('Ym'));

This query will extract the Year and month from the date given column like 201605 (2016-05-21)

display record with specific date in codeigniter

Use Date variable like '$date' instead of $date. Also make sure date format are same

public function spesific_odontogram($id_pasien, $date){
$query = $this->db->query("SELECT * FROM odontogram WHERE inserted_at = '$date' AND id_pasien=$id_pasien");

if ($query->row_array()>0) {
return $query->result_array();
return false;

changing datetime to date for comparison in codeigniter

Another option is to use the SQL BETWEEN function to compare dates but you'd need to convert the incoming dates to the date format: YYYY-MM-DD

$from = date('Y-m-d', strtotime($from_date));
$to = date('Y-m-d', strtotime($to_date));
$search_where = 'CAST(merchant_transactions.datetime As Date) BETWEEN "'.$from.'" AND "'.$to.'"';

Related Topics

Leave a reply