473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Date format

I am going crazy on how to format a simple date. I have this in my MySQL
table. "2005-03-10 08:44:21" and I want it to format out like "3/10/2005" or
"03/10/05" but I seem to be going at it the wrong way. Here what I have but
wow I get a wrong date.

$cdate = date("r",$cdate);

Please help!! I appreciate any help you can give.

Rick
Jul 17 '05 #1
11 4050
Am Sat, 12 Mar 2005 17:48:32 +0000 schrieb Rick:
I am going crazy on how to format a simple date. I have this in my MySQL
table. "2005-03-10 08:44:21" and I want it to format out like "3/10/2005" or
"03/10/05" but I seem to be going at it the wrong way. Here what I have but
wow I get a wrong date.

$cdate = date("r",$cdate);

try `$cdate = date("m/d/y",$cdate);'

Christian.

--
http://sagmueller.net

Jul 17 '05 #2
On Sat, 12 Mar 2005 17:48:32 GMT, "Rick" <ri**@di-wave.com> wrote:
I am going crazy on how to format a simple date. I have this in my MySQL
table. "2005-03-10 08:44:21" and I want it to format out like "3/10/2005" or
"03/10/05" but I seem to be going at it the wrong way. Here what I have but
wow I get a wrong date.

$cdate = date("r",$cdate);


date() works on UNIX timestamps, not string representations of dates.

Either fetch a UNIX timestamp from MySQL (I think it's TO_UNIXTIME or
similar), or get MySQL to format it for you using DATE_FORMAT in the SQL
statement.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #3
Rick wrote:
I am going crazy on how to format a simple date. I have this in my MySQL
table. "2005-03-10 08:44:21" and I want it to format out like "3/10/2005" or
"03/10/05" but I seem to be going at it the wrong way.


Let MySQL do the work.

First format: SELECT DATE_FORMAT(your_date_field, '%e/%c/%Y') FROM ...
Second format: SELECT DATE_FORMAT(your_date_field, '%d/%c/%y') FROM ...

http://dev.mysql.com/doc/mysql/en/da...functions.html

JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #4
Andy Hassall wrote:
On Sat, 12 Mar 2005 17:48:32 GMT, "Rick" <ri**@di-wave.com> wrote:
I am going crazy on how to format a simple date. I have this in my
MySQL table. "2005-03-10 08:44:21" and I want it to format out like
"3/10/2005" or "03/10/05" but I seem to be going at it the wrong way.
Here what I have but wow I get a wrong date.

$cdate = date("r",$cdate);


date() works on UNIX timestamps, not string representations of dates.

Either fetch a UNIX timestamp from MySQL (I think it's TO_UNIXTIME or
similar), or get MySQL to format it for you using DATE_FORMAT in the
SQL statement.


Or you can convert the MySQL datetime value into a unix timestamp in PHP
using the strtotime() function
eg $cdate = date("r", strtotime($cdate));

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #5
WOW that was easy thanks very much!!!

Thank you everyone that helped!!

Rick

"Chris Hope" <bl*******@electrictoolbox.com> wrote in message
news:39*************@individual.net...
Andy Hassall wrote:
On Sat, 12 Mar 2005 17:48:32 GMT, "Rick" <ri**@di-wave.com> wrote:
I am going crazy on how to format a simple date. I have this in my
MySQL table. "2005-03-10 08:44:21" and I want it to format out like
"3/10/2005" or "03/10/05" but I seem to be going at it the wrong way.
Here what I have but wow I get a wrong date.

$cdate = date("r",$cdate);


date() works on UNIX timestamps, not string representations of dates.

Either fetch a UNIX timestamp from MySQL (I think it's TO_UNIXTIME or
similar), or get MySQL to format it for you using DATE_FORMAT in the
SQL statement.


Or you can convert the MySQL datetime value into a unix timestamp in PHP
using the strtotime() function
eg $cdate = date("r", strtotime($cdate));

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

Jul 17 '05 #6
Andy Hassall wrote:
On Sat, 12 Mar 2005 17:48:32 GMT, "Rick" <ri**@di-wave.com> wrote:
I am going crazy on how to format a simple date. I have this in my MySQL
table. "2005-03-10 08:44:21" and I want it to format out like "3/10/2005"
or "03/10/05" but I seem to be going at it the wrong way. Here what I have
but wow I get a wrong date.

$cdate = date("r",$cdate);


date() works on UNIX timestamps, not string representations of dates.

Either fetch a UNIX timestamp from MySQL (I think it's TO_UNIXTIME or
similar), or get MySQL to format it for you using DATE_FORMAT in the SQL
statement.


or $cdate=date("r",strtotime($cdate));

strtotime is cool.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #7
.oO(Kenneth Downs)
Andy Hassall wrote:
Either fetch a UNIX timestamp from MySQL (I think it's TO_UNIXTIME or
similar), or get MySQL to format it for you using DATE_FORMAT in the SQL
statement.

or $cdate=date("r",strtotime($cdate));

strtotime is cool.


DATE_FORMAT() is cooler.

Micha
Jul 17 '05 #8
Michael Fesser wrote:
.oO(Kenneth Downs)
Andy Hassall wrote:
Either fetch a UNIX timestamp from MySQL (I think it's TO_UNIXTIME or
similar), or get MySQL to format it for you using DATE_FORMAT in the SQL
statement.

or $cdate=date("r",strtotime($cdate));

strtotime is cool.


DATE_FORMAT() is cooler.

Micha


Yeah, but its a DB function.

<opinion category="mild rant">
My own tastes run to having PHP as the middleman do all of the formatting.
In the one direction PHP takes user input and turns it into formatted SQL
writes (insert, update, etc) and in the other direction it takes db-native
data from a SELECT and formats it for human consumption.

If you have a data dictionary, you write two simple functions that take care
of all of this for you:

function SQL_FORMAT($user_value,$type) // makes SQL-acceptable strings...
function HTML_FORMAT($sql_value,$type) // makes user-readable strings...

Where $type is pulled from the dd. If different sites and/or users want
different date display styles, you can have HTML_FORMAT read a preferences
file.

While there is nothing "wrong" with DATE_FORMAT(), it would muddy this clean
placement of responsibilities.
</opinion>

but of course to each his own.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #9
kn**************@see.sigblock says...
Michael Fesser wrote:
DATE_FORMAT() is cooler.
Yeah, but its a DB function.

<opinion category="mild rant">
My own tastes run to having PHP as the middleman do all of the formatting.

<snip> </opinion>

but of course to each his own.


<alt_opinion category="mild rant">
In general, the majority (as I mainly work with Oracle this can read
*overwhelming, approaching total* majority) of functions which can be
executed at both the database and application level will run *much* more
efficiently at the database level.

Unless you have a critical need for database vendor obfuscation (and
therefore accept that you will be producing less than optimal code), my
rule is:
"if it can be done by the database, let the database do it".
</alt_opinion>

Geoff M
Jul 17 '05 #10
Geoff M wrote:
kn**************@see.sigblock says...
Michael Fesser wrote:
> DATE_FORMAT() is cooler.


Yeah, but its a DB function.

<opinion category="mild rant">
My own tastes run to having PHP as the middleman do all of the
formatting.

<snip>
</opinion>

but of course to each his own.


<alt_opinion category="mild rant">
In general, the majority (as I mainly work with Oracle this can read
*overwhelming, approaching total* majority) of functions which can be
executed at both the database and application level will run *much*
more efficiently at the database level.

Unless you have a critical need for database vendor obfuscation (and
therefore accept that you will be producing less than optimal code),
my rule is:
"if it can be done by the database, let the database do it".
</alt_opinion>


And even if you have a preferences file as in Kenneth's case you could
still use it to format the date in the SQL statement, since you are
creating the statement in PHP.

I've always tended to do all the formatting in PHP but your argument
does make sense as it should presumably be more efficient for the
database server to format the date and time, considering it has to
anyway in order to get it into whatever format it spits out by default.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #11
.oO(Chris Hope)
And even if you have a preferences file as in Kenneth's case you could
still use it to format the date in the SQL statement, since you are
creating the statement in PHP.


Yep. Additionally that avoids the use of Unix timestamps, which have
their own limitations.

Micha
Jul 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
2
by: amith | last post by:
hi I have written javascript for comparing two dates in US format and finding out whether the start date is greater than the end date and vice versa. In this attempt i have instantiated the...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
3
by: Lyn | last post by:
Hi, I am developing a project in which I am checking for records with overlapping start/end dates. Record dates must not overlap date of birth, date of death, be in the future, and must not...
5
by: Macca | last post by:
Hi, I have a table which has a date/time field. I am storing them as follows :- 01/01/2005 11:25 01/01/2005 19:44 02/01/2005 05:04
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
20
by: andreas | last post by:
When I copy a vb.net project using date formats from one PC with a windows date format f.e. dd/mm/yyyy to another PC having a format yy/mm/dd then I get errors. How can I change for a while in the...
30
by: fniles | last post by:
On my machine in the office I change the computer setting to English (UK) so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US. This problem happens in either Access or SQL Server. In the...
4
by: OzNet | last post by:
I have some functions to calculate the working days in a given period. This includes a table that is queried to calculate the number of public holidays that don’t occur on a weekend. If I test...
11
ollyb303
by: ollyb303 | last post by:
Hello, I am using a dynamic crosstab report to track performance statistics for my company and I have hit a problem. I would like the option to track stats daily (for the last 7 complete...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.