ldoozer Posted November 28, 2006 Share Posted November 28, 2006 Hi all, can anyone tell me the best way to only display the date in the output of a datetime mysql field.So I have 2006-11-28 20:54:46 stored but only want to display 2006-11-28. Link to comment https://forums.phpfreaks.com/topic/28788-trim-output-of-datetime-field/ Share on other sites More sharing options...
The Little Guy Posted November 28, 2006 Share Posted November 28, 2006 [CODE]<?php$date = '2006-11-28 20:54:46';list($year,$time) = explode(" ",$date);echo $year;?>[/CODE] Link to comment https://forums.phpfreaks.com/topic/28788-trim-output-of-datetime-field/#findComment-131790 Share on other sites More sharing options...
roopurt18 Posted November 28, 2006 Share Posted November 28, 2006 Alternatively you can tell MySQL to return only that part of the field and avoid modifying it in PHP altogether. Link to comment https://forums.phpfreaks.com/topic/28788-trim-output-of-datetime-field/#findComment-131802 Share on other sites More sharing options...
ldoozer Posted November 28, 2006 Author Share Posted November 28, 2006 how would i do that? (embarrased) Link to comment https://forums.phpfreaks.com/topic/28788-trim-output-of-datetime-field/#findComment-131817 Share on other sites More sharing options...
roopurt18 Posted November 28, 2006 Share Posted November 28, 2006 http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.htmlDATE([i]expr[/i])SELECT DATE('2003-12-31 01:02:03')So if you have a table [i]the_table[/i] and the DATETIME column is named [i]the_date[/i]:SELECT *, DATE(the_date) AS the_date_disp FROM the_table WHERE 1Also, if you want to format the date like you can with PHP's date function, you'll want to look up DATE_FORMAT in MySQL.SELECT * DATE_FORMAT(the_date, '%a. %b. %D, %Y') AS the_date_disp FROM the_table WHERE 1Hope that helps. Link to comment https://forums.phpfreaks.com/topic/28788-trim-output-of-datetime-field/#findComment-131827 Share on other sites More sharing options...
ldoozer Posted November 30, 2006 Author Share Posted November 30, 2006 Thanks very much for all the help. :) Link to comment https://forums.phpfreaks.com/topic/28788-trim-output-of-datetime-field/#findComment-132953 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.