Solved

Removing 00:00:00 from date() output

Posted on 2011-03-23
5
340 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 166 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 45

Accepted Solution

by:
Kdo earned 167 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 167 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now