Solved

Removing 00:00:00 from date() output

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

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
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

773 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