66

On my database under the t_stamp columns I have the date for example

2013-11-26 01:24:34

From that same column I only want to get the date

2013-11-26

How can i do this? Thank You!

4 Answers4

117

You can use date(t_stamp) to get only the date part from a timestamp.

You can check the date() function in the docs

DATE(expr)

Extracts the date part of the date or datetime expression expr.

mysql> SELECT DATE('2003-12-31 01:02:03'); -> '2003-12-31'

Filipe Silva
  • 21,189
  • 5
  • 53
  • 68
4

You can convert that time in Unix timestamp by using

select UNIX_TIMESTAMP('2013-11-26 01:24:34')

then convert it in the readable format in whatever format you need

select from_unixtime(UNIX_TIMESTAMP('2013-11-26 01:24:34'),"%Y-%m-%d");

For in detail you can visit link

Dilraj Singh
  • 951
  • 10
  • 12
2

To get only date from timestamp in MySQL just use DATE_FORMAT(datetime, '%Y-%m-%d'):

SELECT DATE_FORMAT('2013-11-26 01:24:34', '%Y-%m-%d');
> 2013-11-26
buddemat
  • 4,552
  • 14
  • 29
  • 49
-8
$date= new DateTime($row['your_date']) ;  
echo $date->format('Y-m-d');
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 4
    While this code snippet may solve the problem, [including an explanation](//meta.stackexchange.com/q/114762) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Cody Gray - on strike Oct 28 '17 at 10:31
  • 2
    THIS SNIPPET IS FOR `php` WHERE THE POST ASKED FOR `mysql` – user306 Nov 03 '20 at 04:36