473,804 Members | 3,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting Date Formats?

Kit
Howdy!

I have some dates in a MySQL database that are in a 'Y-m-d' format.
Is there a way to have PHP read these dates and convert them to a
'd-M-Y' format?
Jul 17 '05 #1
6 13153

On 11-Nov-2003, ad****@yahoo.co m (Kit) wrote:
I have some dates in a MySQL database that are in a 'Y-m-d' format.
Is there a way to have PHP read these dates and convert them to a
'd-M-Y' format?


You can use mysql to convert them to some other format with the
DATE_FORMAT() function or, if they're in the UNIX timestamp date range, you
can use the mysql UNIX_TIMESTAMP or the PHP strtotime() function to convert
them to a UNIX timestamp and the date() function to convert the timestamp to
anything you want. for example:

select *, date_format(dat efield,'%d-%m-%Y') as dmydate from ...

or

$dmydate = date('d-m-Y',strtotime($r ow['datefield']));

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@wil lglen.net (it's reserved for spammers)
Jul 17 '05 #2
*** Kit wrote/escribió (11 Nov 2003 23:15:42 -0800):
I have some dates in a MySQL database that are in a 'Y-m-d' format.
Is there a way to have PHP read these dates and convert them to a
'd-M-Y' format?


There are many ways. For instance, database can make the task for you:

select date_format(NOW (), '%d-%m-%Y')

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #3
ad****@yahoo.co m (Kit) writes:
I have some dates in a MySQL database that are in a 'Y-m-d' format.
Is there a way to have PHP read these dates and convert them to a
'd-M-Y' format?


Here's one way, assuming that the 'Y-m-d' date is in the variable
$ymd:

$dmy = date('d-M-Y', strtotime($ymd) );

Is there a reason you can't have MySQL format the date the way you
want it with DATE_FORMAT()?

mysql> select birthday from foo;
+------------+
| birthday |
+------------+
| 1997-01-12 |
+------------+
1 row in set (0.00 sec)

mysql> select date_format(bir thday, '%d-%b-%Y') from foo;
+-----------------------------------+
| date_format(bir thday, '%d-%b-%Y') |
+-----------------------------------+
| 12-Jan-1997 |
+-----------------------------------+
1 row in set (0.00 sec)

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 17 '05 #4
ad****@yahoo.co m (Kit) wrote in message news:<e7******* *************** ****@posting.go ogle.com>...
Howdy!

I have some dates in a MySQL database that are in a 'Y-m-d' format.
Is there a way to have PHP read these dates and convert them to a
'd-M-Y' format?


Take a look at http://www.mysql.com/doc/en/Date_and...functions.html
particularly the bit about the DATE_FORMAT function.

Or take a look at http://www.tonymarston.net/php-mysql/dateclass.html
which describes a PHP class which can validate and format dates.

Tony Marston
http://www.tonymarston.net/
Jul 17 '05 #5
"Michael Fuhr" <mf***@fuhr.org > schrieb im Newsbeitrag
news:3f******** @omega.dimensio nal.com...
ad****@yahoo.co m (Kit) writes:
Is there a reason you can't have MySQL format the date the way you
want it with DATE_FORMAT()?


If you want to order by the date you can't do that.

Though the other suggestions are more elegant you might like that solution
for your special case:

$d = explode("-", $date);
$formatteddate = $d[2]."-".$d[1]."-".$d[0];

HTH
Markus
Jul 17 '05 #6
"Markus Ernst" <derernst@NO#SP #AMgmx.ch> writes:
"Michael Fuhr" <mf***@fuhr.org > schrieb im Newsbeitrag
news:3f******** @omega.dimensio nal.com...
Is there a reason you can't have MySQL format the date the way you
want it with DATE_FORMAT()?


If you want to order by the date you can't do that.


If you want to sort by date in PHP then changing the date format
can make the sorting awkward, but using ORDER BY in the query works
just fine:

mysql> -- Unordered query
mysql> select name, date_format(bir thday, '%d-%b-%Y') from people;
+---------+-----------------------------------+
| name | date_format(bir thday, '%d-%b-%Y') |
+---------+-----------------------------------+
| Matthew | 20-Nov-1997 |
| David | 06-Jul-1993 |
| Scott | 17-Feb-1992 |
| John | 03-Nov-1997 |
| Susan | 05-Apr-2001 |
| George | 13-Aug-2000 |
| Henry | 09-Jul-1991 |
| Mary | 16-Jul-2000 |
| Thomas | 14-May-1992 |
| Robert | 20-Mar-1994 |
+---------+-----------------------------------+
10 rows in set (0.00 sec)

mysql> -- Ordered query
mysql> select name, date_format(bir thday, '%d-%b-%Y') from people order by birthday;
+---------+-----------------------------------+
| name | date_format(bir thday, '%d-%b-%Y') |
+---------+-----------------------------------+
| Henry | 09-Jul-1991 |
| Scott | 17-Feb-1992 |
| Thomas | 14-May-1992 |
| David | 06-Jul-1993 |
| Robert | 20-Mar-1994 |
| John | 03-Nov-1997 |
| Matthew | 20-Nov-1997 |
| Mary | 16-Jul-2000 |
| George | 13-Aug-2000 |
| Susan | 05-Apr-2001 |
+---------+-----------------------------------+
10 rows in set (0.01 sec)

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 17 '05 #7

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

Similar topics

8
9449
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $ Last-Modified: $Date: 2003/10/28 19:48:44 $ Author: A.M. Kuchling <amk@amk.ca> Status: Draft Type: Standards Track
4
5395
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 calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
6
4841
by: Jim Davis | last post by:
Before I reinvent the wheel I thought I'd ask: anybody got a code snippet that will convert the common ISO8601 date formats to a JS date? By "common" I mean at the least ones described in this W3C note: http://www.w3.org/TR/NOTE-datetime For my requirements the code would be need to be open sourceable under the BSD license.
5
8713
by: Cameron | last post by:
I accept a date from a user in a textbox, but I need to convert it to a DateTime. I have the following code: return DateTime.ParseExact("01/10/2003", "dd/MM/yyyy", new CultureInfo("en-GB")); When I run it on my development PC it's fine - I get 1st October 2003. When I run it from my webserver it doesn't work - I get 10th January 2003.
4
3337
by: Dan Lewis | last post by:
I've imported a ms access database into a table in a mysql database. The access database contains a field that holds date/time values in 'general date' format. These all show up at 01/01/1970 in the mysql database. I believe the field in mysql is wanting UTC and shows numbers when looked at from the sql command line (i.e. March 13, 2006, 5:31 pm is shown as 1142289086). How do I get the access data into that format so it will import...
12
48232
by: Vincent Delporte | last post by:
Hello My site is hosted on a server in the US, hence set up to use the mm/dd/yyyy date format instead of the European dd/mm/yyyy. Also, MySQL stores dates as yyyy-mm-dd, so I need to convert dd/mm/yyyy to that format. I'm a newbie, and didn't find the answer through Google :-/ Do I need to call another function in addition to strtotime() to make
18
13050
by: Dirk Hagemann | last post by:
Hello, From a zone-file of a Microsoft Active Directory integrated DNS server I get the date/time of the dynamic update entries in a format, which is as far as I know the hours since january 1st 1901. For Example: the number 3566839 is 27.11.07 7:00. To calculate this in Excel I use this: ="01.01.1901"+(A1/24-(REST(A1;24)/24))+ZEIT(REST(A1;24);0;0) (put 3566839 in field A1 and switch the format of the result-field to the corresponding...
4
1742
by: nikos | last post by:
Hi, I have a table with one of the columns configured to record dates as ('#', 'MM/DD/YYYY'). However, two entries in that column are of format YYYY and MM/YYYY. How can I change only those two entries preferably to Date but in formats above? i tried to do: "update table_name set column_name = to_date('2008', 'YYYY') where c_id = 12"
9
1580
by: SOLVER | last post by:
Hi, I can't figure out why this is not working propelly: date("d.m.Y",strtotime("05/03/2008 00:00:00")); result: 03.05.2008 date("d.m.Y",strtotime("18/05/2008 00:00:00")); result: 05.06.2009 (From where this come from?) What I'm doing wrong?
0
9708
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10327
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.