Solved

Removing 00:00:00 from date() output

Posted on 2011-03-23
5
326 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

757 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

14 Experts available now in Live!

Get 1:1 Help Now