Solved

Removing 00:00:00 from date() output

Posted on 2011-03-23
5
357 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:
Kent Olsen 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Email called spam 5 37
Where on a calculated field 1 23
Log in through ID 5 19
Wordpress - Adding an if/else Conditional Argument in a Post Template 4 14
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

821 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