Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Removing 00:00:00 from date() output

Posted on 2011-03-23
5
Medium Priority
?
383 Views
Last Modified: 2012-08-14
hi,
I'm trying to put the date into a mysql row, and it is keeping the "00:00:00" which looks unprofessional. I've tried to get rid of it using str_replace() and just tried explode() and threw in the first element of the array, as you can see, still with no luck.

is mysql automatically appending the "00:00:00" to the end of the date? I don't understand why I can't get rid of it.

Thanks!
$date = date("Y-m-d");
			$fdate = explode(" ",$date);
				$result = mysql_query("INSERT INTO `clients` (`id`, `site`, `login`, `pass`, `date`, `ip`) VALUES (NULL , '".$cleanurl."', '".$username."', '".$password."', '".$fdate[0]."', '".$_SERVER['REMOTE_ADDR']."');", $mysql);

Open in new window

0
Comment
Question by:JoeD77
5 Comments
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 664 total points
ID: 35202561
If the MySQL column is a DATETIME field then it always needs to hold a time and if you don't supply one it will add its own.

"keeping the "00:00:00" which looks unprofessional"
en
Why? It's in the database and end users never see the database data - it is always reformatted for output to HTML anyway. If you want to lose the time portion then alter the table to have that column as a DATE field rather than a DATETIME
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 668 total points
ID: 35202571
Hi Joe,

There is a huge difference between what MySQL stores and what it displays.  If may store a date, but that has no impact on the format that you choose for its display.  The same holds true for a datetime.

I suspect that the data type of the database column is a datetime (or timestamp).  All that is stored in the column is a date.  MySQL is performing its due dilligence and formatting a date time.

Check the data type.  You can always cast the datetime to a date, or just grab the first 10 characters of the formatted datetime.


Good Luck,
Kent
0
 
LVL 4

Assisted Solution

by:m_walker
m_walker earned 668 total points
ID: 35202582
Check out the database field types.  If the field type is a "date only" field then it wont store the time.  But if its a "date time" field, then it must store the time, since you did not supply the time bit it will be set to 00:00:00.

You have 2 choices.  
a) drop the time when retrieve the date prior to display (either in code or by a date trunc function.
b) change the db filed for a data only field type.

field types
DATE
 A date
 The supported range is ‘1000-01-01’ to ‘9999-12-31’. MySQL displays DATE values in ‘YYYY-MM-DD’ format
 
DATETIME
 A date and time combination
 The supported range is ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’. MySQL displays DATETIME values in ‘YYYY-MM-DD HH:MM:SS’ format
 
0
 
LVL 4

Expert Comment

by:m_walker
ID: 35202595
In sql you can use the date function to drop the time bit

•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'
0
 

Author Closing Comment

by:JoeD77
ID: 35202769
Thanks +D
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question