By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,017 Members | 1,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,017 IT Pros & Developers. It's quick & easy.

How to format at date field output

P: n/a
I have a field in a database called DateRcvd. At present, it outputs in my
report in the yyyy-mm-dd format. I would like it to display in the dd/mm/yy
format. What is the easiest way to accomplish this?
Jul 25 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Bob Sanderson wrote:
I have a field in a database called DateRcvd. At present, it outputs in my
report in the yyyy-mm-dd format. I would like it to display in the dd/mm/yy
format. What is the easiest way to accomplish this?
Depends on the database., But most have a function to format the
default date to however you want. Check your database documentation.

Otherwise you could reformat it in the php code - use substr() or a
regex to get the year, month and day, then display them like you wish.

Personally I prefer the database function.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 25 '06 #2

ronverdonk
Expert 2.5K+
P: 4,258
[PHP]<?php
$d="2006-10-25";
echo date("d/m/y", strtotime($d));
?>[/PHP]
results in 25/10/06

Ronald :cool:
Jul 25 '06 #3

P: n/a
Bob Sanderson wrote:
I have a field in a database called DateRcvd. At present, it outputs
in my report in the yyyy-mm-dd format. I would like it to display in
the dd/mm/yy format. What is the easiest way to accomplish this?
You could reformat it using PHP:

echo date("d/m/y",strtotime($row["DateRcvd"]));

http://php.net/date
http://php.net/strtotime

--
Kim André Akerø
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Jul 25 '06 #4

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Bob Sanderson wrote:
I have a field in a database called DateRcvd. At present, it outputs in my
report in the yyyy-mm-dd format. I would like it to display in the
dd/mm/yy format. What is the easiest way to accomplish this?
Rely on the DB's date formatting functions. Something like:

select date_format( table.DateRcvd, '%y/%m/%d' ) as formatted_date from
table;

Please check your SQL DB manual for the exact syntax for those functions.
- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)

iD8DBQFExh5u3jcQ2mg3Pc8RAnR3AJ9HyyXYCrNFijuaLPdffe RuYzNBagCcCcNW
Xn0pEQgp9bdzIalxackv3RE=
=EF4e
-----END PGP SIGNATURE-----
Jul 25 '06 #5

ronverdonk
Expert 2.5K+
P: 4,258
Sorry I forgot, but when you want to use MySql to format it, just do:
Expand|Select|Wrap|Line Numbers
  1. select date , date_format(date, "%d/%m/%y") as date1 from table
then date1 contains your re-formatted date.

Ronald :cool:
Jul 25 '06 #6

P: n/a
On Tue, 25 Jul 2006 09:46:02 -0400, Jerry Stuckle wrote:
>I have a field in a database called DateRcvd. At present, it outputs in
my report in the yyyy-mm-dd format. I would like it to display in the
dd/mm/yy format. What is the easiest way to accomplish this?

Depends on the database., But most have a function to format the default
date to however you want. Check your database documentation.

Otherwise you could reformat it in the php code - use substr() or a regex
to get the year, month and day, then display them like you wish.

Personally I prefer the database function.
Personally I prefer Kim André Akerø's solution, it's easier to read and
programmer time is (in *most* of the work I do) worth more than CPU time.
However, Kim's method is by far the slowest. To do 10,000 iterations of
each version:

$ ./test.php
strtotime/date = 0.5179 seconds
ereg = 0.0885 seconds
preg = 0.0435 seconds
substr = 0.0282 seconds

However, as I rarely do more than one or two conversions like this per
page impression, and they are on fairly low traffic sites on powerful
boxes (max is about 5M page impressions per month) I'll stick to most
readable :-)

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jul 25 '06 #7

P: n/a
Andy Jeffries wrote:
>
Personally I prefer Kim André Akerø's solution, it's easier to read and
programmer time is (in *most* of the work I do) worth more than CPU time.
However, Kim's method is by far the slowest. To do 10,000 iterations of
each version:
Formatting within the PHP script might be easier to read, but databases
usually have extensive built-in calendaring capabilities. At least, that
is the case with Oracle (my favorite), SQL Server and PostgreSQL. All
the date rules are already built in, database "knows" what day will it
be on 7/4/2076, will it be a leap year and what day will Halloween fall
on this year. I find it wasteful to re-implement all those goodies in
PHP, just for purity.
--
Mladen Gogala
http://www.mgogala.com
Jul 25 '06 #8

P: n/a
*** Bob Sanderson escribió/wrote (Tue, 25 Jul 2006 12:49:17 GMT):
I have a field in a database called DateRcvd. At present, it outputs in my
report in the yyyy-mm-dd format. I would like it to display in the dd/mm/yy
format. What is the easiest way to accomplish this?
I particularly find it easier to handle dates as Unix timestamps. You don't
say what your DBMS is so I'll assume MySQL:

SELECT UNIX_TIMESTAMP(DateRcvd) FROM table

Once you read the value into a PHP variable, you can use almost date
function to format it, such as date() or strftime().

When you need to insert dates into MySQL:

INSERT INTO table (DateRcvd) VALUES (FROM_UNIXTIME(1153851613))
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Jul 25 '06 #9

P: n/a
Andy Jeffries wrote:
On Tue, 25 Jul 2006 09:46:02 -0400, Jerry Stuckle wrote:
>>>I have a field in a database called DateRcvd. At present, it outputs in
my report in the yyyy-mm-dd format. I would like it to display in the
dd/mm/yy format. What is the easiest way to accomplish this?

Depends on the database., But most have a function to format the default
date to however you want. Check your database documentation.

Otherwise you could reformat it in the php code - use substr() or a regex
to get the year, month and day, then display them like you wish.

Personally I prefer the database function.


Personally I prefer Kim André Akerø's solution, it's easier to read and
programmer time is (in *most* of the work I do) worth more than CPU time.
However, Kim's method is by far the slowest. To do 10,000 iterations of
each version:

$ ./test.php
strtotime/date = 0.5179 seconds
ereg = 0.0885 seconds
preg = 0.0435 seconds
substr = 0.0282 seconds

However, as I rarely do more than one or two conversions like this per
page impression, and they are on fairly low traffic sites on powerful
boxes (max is about 5M page impressions per month) I'll stick to most
readable :-)

Cheers,
Andy

Andy,

Personally, I prefer to let the database handle it. It's generally
faster overall - the database probably had to convert it to yyyy-mm-dd
format in the first place. That way it can convert directly to the
desire format instead of going through two conversions.

And even if it doesn't have to convert it to yyyy-mm-dd, the compiled
code in the database is generally a lot more efficient than the PHP.

And that's the easiest to read!

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 25 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.