Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL select date with a format in codeigniter

My Codeigniter site select date from a table as follows.

$this->db->select('reg_id, fname, lname, added_date');// working

MYSQL database has added_dateformat as 2016-09-10 14:44:46 Now i want to select data from table format should be 2016-09-10 14:44 PM

selecting date separately can get the format

SELECT DATE_FORMAT(added_date, '%Y-%m-%d %h:%i %p') FROM tble_reg

Then how to select other columns with added_date relevant format

$this->db->select('reg_id, fname, lname, DATE_FORMAT(added_date, '%Y-%m-%d %h:%i %p')'); 
//not working
like image 401
Geeth Welagedara Avatar asked Nov 30 '25 02:11

Geeth Welagedara


2 Answers

This is speculation, but you might have the single quotes competing with each other. Does this work?

$this->db->select('reg_id, fname, lname, DATE_FORMAT(added_date, "%Y-%m-%d %h:%i %p") as added_date');

MySQL allows both single quotes and double quotes for string delimiters, which is handy in cases when single quotes are needed for delimiting strings at the application layer.

like image 190
Gordon Linoff Avatar answered Dec 01 '25 18:12

Gordon Linoff


Brake the line in to two select statements

$this->db->select('reg_id, fname, lname');
$this->db->select(DATE_FORMAT(added_date, '%Y-%m-%d %h:%i %p') AS added_date);

Or use " with Date Format

$this->db->select('reg_id, fname, lname, DATE_FORMAT(added_date, "%Y-%m-%d %h:%i %p")  AS added_date ');
like image 40
Abdulla Nilam Avatar answered Dec 01 '25 16:12

Abdulla Nilam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!