473,394 Members | 1,956 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,394 software developers and data experts.

Dates

Hi all, I have a date entered into a mysql database in the format:
yyyy-mm-dd. I want to retrieve the date and display it in a different format
using php. For example, if the mysql date entered is: 2005-03-08, I would
like to display it on a webpage as:

March 8th, 2005

Any ideas? I have read up on the php date() function but I only know how to
get the current date and not how to format/display existing dates in
different formats.

Thanks in advance,

Mosher
Jul 17 '05 #1
12 1579
.oO(Mosher)
Hi all, I have a date entered into a mysql database in the format:
yyyy-mm-dd. I want to retrieve the date and display it in a different format
using php. For example, if the mysql date entered is: 2005-03-08, I would
like to display it on a webpage as:

March 8th, 2005

Any ideas?


Have a look at MySQL's DATE_FORMAT() function.

Micha
Jul 17 '05 #2
Mosher wrote:
Hi all, I have a date entered into a mysql database in the format:
yyyy-mm-dd. I want to retrieve the date and display it in a different
format using php. For example, if the mysql date entered is:
2005-03-08, I would like to display it on a webpage as:

March 8th, 2005

Any ideas? I have read up on the php date()


Don't. Most databases support several date formatting functions. Every time
you use a database ask yourself: can the database do this? I have seen a
lot of programs that are cluttered with code that just repeats
functionality that is present in the database.

If you are serious at using MySQL take some time to carefully read through
all the functions it supports. Make notes regarding the handy ones, and the
ones you *think* you will never use ;-)

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #3
Guys, I did a little more investigating and found my solution using the
"explode" function in PHP. My solution looks something like this:

$row_date = explode("-", $row[event_date]);
$date = date('F jS, Y',
mktime(0,0,0,$row_date[1],$row_date[2],$row_date[0]));

Thanks for your help!

Mosher

"Mosher" <mo***********@yahoo.com> wrote in message
news:ZY********************@comcast.com...
Hi all, I have a date entered into a mysql database in the format:
yyyy-mm-dd. I want to retrieve the date and display it in a different
format using php. For example, if the mysql date entered is: 2005-03-08, I
would like to display it on a webpage as:

March 8th, 2005

Any ideas? I have read up on the php date() function but I only know how
to get the current date and not how to format/display existing dates in
different formats.

Thanks in advance,

Mosher

Jul 17 '05 #4
.oO(Mosher)
Guys, I did a little more investigating and found my solution using the
"explode" function in PHP. My solution looks something like this:
It's ugly.
$row_date = explode("-", $row[event_date]);
$date = date('F jS, Y',
mktime(0,0,0,$row_date[1],$row_date[2],$row_date[0]));


What's wrong with letting the database do the work for you? You just
have to add a call to DATE_FORMAT() to your query, that's all. No need
for explode(), mktime() and date().

Micha
Jul 17 '05 #5
Mosher wrote:
Guys, I did a little more investigating
I doubt it.
and found my solution using
the "explode" function in PHP.
Which is the worst possible solution.
My solution looks something like this:

$row_date = explode("-", $row[event_date]);
$date = date('F jS, Y',
mktime(0,0,0,$row_date[1],$row_date[2],$row_date[0]));


3 lines of code which can be done with just one MySQL function...

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #6
Mosher wrote:
Hi all, I have a date entered into a mysql database in the format:
yyyy-mm-dd. I want to retrieve the date and display it in a different format
using php. For example, if the mysql date entered is: 2005-03-08, I would
like to display it on a webpage as:

March 8th, 2005

Any ideas? I have read up on the php date() function but I only know how to
get the current date and not how to format/display existing dates in
different formats.

SELECT DATE_FORMAT(your_date_field, '%M %D, %Y') FROM your_table

See <http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html>.

HTH,
JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #7
If you lived in a country where there are 3 official languages, and
thus 3 different ways to display a date, you'd understand that this
solution is not a good one. It's not the database's job to handle the
presentation aspects of the application. You should'nt mix presentation
code with data access code.
The best way to do this is to transform the date into a unix timestamp,
and then use the function strftime
(http://fr3.php.net/manual/en/function.strftime.php).
To transform the MySQL date into a timestamp, use the php funtion
strtotime, or the MySQL function UNIX_TIMESTAMP as described here:
http://fr.php.net/manual/en/function.date.php

JB.

Jul 17 '05 #8
.oO(Jean-Baptiste Nizet)
If you lived in a country where there are 3 official languages, and
thus 3 different ways to display a date, you'd understand that this
solution is not a good one.
No problem with that. Set the format string in DATE_FORMAT() according
to the reqested language.
It's not the database's job to handle the
presentation aspects of the application. You should'nt mix presentation
code with data access code.
Quite true, but I don't consider that an issue in this case. Assume a
database containing texts in multiple languages. The application simply
calls "get me this and that text". The DB code has to know the requested
language in order to return the correct variant. And if the code is able
to return text in different languages it should also be able to return a
date in different formats.
The best way to do this is to transform the date into a unix timestamp,
and then use the function strftime
(http://fr3.php.net/manual/en/function.strftime.php).


I never consider Unix timestamps the best solution, simply because of
their range restriction. While it will work in many cases, it will fail
in others.

Micha
Jul 17 '05 #9
Mosher wrote:
March 8th, 2005


I prefer '8 March 2005'. The comma is then unnecessary
because the name of the month separates the numerals, and
the expression runs in order of significance. Also, the
ordinal suffix is superfluous; 'March 8th, 2005' says
nothing that 'March 8, 2005' doesn't.

--
Jock
Jul 17 '05 #10
And where are you going to perform the reverse operation: parse a date
entered by a user to put it in the database? Are you goinf to use the
MySQL function STR_TO_DATE()? How do you perform validation and return
meaningful error messages then? And if you don't use STR_TO_DATE, don't
you find this asymmetricity a bit strange?
And if you have to display a date which doesn't come from the database,
how are you gonna use the MySQL function? You'll duplicate date
formatting code?
And if you decide to use Oracle, DB2 or PostgreSQL rather than MySQL,
you're gonna change all your presentation code?

Jul 17 '05 #11
.oO(Jean-Baptiste Nizet)
And where are you going to perform the reverse operation: parse a date
entered by a user to put it in the database? Are you goinf to use the
MySQL function STR_TO_DATE()? How do you perform validation and return
meaningful error messages then?
PHP
And if you don't use STR_TO_DATE, don't
you find this asymmetricity a bit strange?
No. I always use the tool that I think is most appropriate and efficient
to solve a particular problem.
And if you have to display a date which doesn't come from the database,
how are you gonna use the MySQL function? You'll duplicate date
formatting code?
For other language-dependent output 'gettext' is an option.
And if you decide to use Oracle, DB2 or PostgreSQL rather than MySQL,
you're gonna change all your presentation code?


How often do you change the used database? Of course you can avoid many
problems with using a full blown DB abstraction layer, but at what cost?
I prefer to make the best use of the features offered by the DBMS. Or in
other words: I prefer efficiency and performance over abstraction.

Micha
Jul 17 '05 #12
On Wed, 9 Mar 2005 14:30:46 -0600, Mosher wrote:
Hi all, I have a date


Congratulations.
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #13

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

Similar topics

8
by: Riley | last post by:
The date fields being saved by a VB program were being saved as #2003-11-22#. For reasons unknown to me these dates began to be saved as "11/22/2003" All of these dates were made dates with the...
7
by: Alistair | last post by:
diary_date = request.form("diary_date") - (from a populated drop down list) strSQL = "SELECT saz_title, saz_text from saz_details where saz_date =#" & diary_date & "#" a response.write...
5
by: PW | last post by:
<rant> Sorry guys, but I just have to whinge. Dates in ASP are a total pain in the butt! I seem to get caught out so many times. I realise its my own fault, but going from the posts in this...
10
by: Colin Steadman | last post by:
I'm a stupid ASP programmer and I dont do Javascript (except for very simple tasks anyway), and I'm in a bit of a predicament. I've used a javascript table sorting script from here: ...
1
by: Don Sealer | last post by:
I have a report that includes 5 different subreports. I'd like to be able to open this report using a date function (Start Date and End Date). I'd like all five subreports to show the data from...
2
by: Rachel Suddeth | last post by:
Is there a way to have the non-selectable dates (those before MinDate and after MaxDate) draw differently so my users can see right away what dates aren't allowed? I'm not seeing it... ...
12
by: Dixie | last post by:
I am trying to calculate the number of workdays between two dates with regards to holidays as well. I have used Arvin Meyer's code on the Access Web, but as I am in Australia and my date format is...
1
by: pitfour.ferguson | last post by:
My dbase has the start date and end date of each visit. How can I ask Access to list the day of the week of the start (easy), end (easy) and, more importantly, the dates of the visit itself - ie...
7
by: evilcowstare via AccessMonster.com | last post by:
Hi, I have searched the forum for answers on this and to be honest as a novice I find it a bit confusing so apologies if it is simple. There are some searches that I want to apply to my database....
2
by: Jim Carlock | last post by:
(1) Does PHP provide any way to handle dates prior to 1980? I know there's problems with Microsoft Windows NT and all Windows NT operating systems will allow a date prior to 1980 to be placed...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.