Kamesh Jungi
Kamesh Jungi

Reputation: 6251

Separating date and time from datetime format mysql

I have an event page that displays events from database. In my database, I have a column with datetime format. is there a way to only get the date or time separate from each other in a select to be displayed in PHP ?

Note : I am looking for anything default/core PHP function for this issue.

Upvotes: 4

Views: 11025

Answers (5)

Tayyeb
Tayyeb

Reputation: 147

date_format Method used for splitting date and time.

SELECT 
       date_format(ColumnName, '%Y-%m-%d') as Date,
       date_format(ColumnName, '%H:%i:%s') as Time
FROM  
        tableName;

Upvotes: 0

Debarth
Debarth

Reputation: 282

Use this query , you will get your desired output :

$sql = "SELECT DATE(datecolumn) as mydate, TIME(datecolumn) as mytime FROM `tablename`";

Upvotes: 9

Girish
Girish

Reputation: 12117

You can use date_format function

SELECT date_format(dateCol, '%Y-%m-%d') as `date`,
date_format(dateCol, '%H:%i:%s') as `time` FROM tableName;

Upvotes: 2

craig1231
craig1231

Reputation: 3867

In MySQL use DATE(insertdate), TIME(insertdate)

Upvotes: 1

colburton
colburton

Reputation: 4715

Just use the mysql functions:

SELECT DATE(column), TIME(column) FROM table

On php-side use date('d.m.y', $column) and date('H:i:s', $column).

Upvotes: 1

Related Questions