Date switching US to UK

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PlateSpin
Forum Newbie
Posts: 9
Joined: Tue Sep 29, 2009 6:15 am

Date switching US to UK

Post by PlateSpin »

I'm a PHP novice so please forgive me.

I have this small bit of PHP code in my HTML file and it prints the date from the MySQL table very nicely.

Quote generated <? print $row["date"];?> valid for 90 days

Problem being I would like to change the date format from US (YYY.mm.dd) to the UK (dd.mm.YYYY)

How hard can it be or have I made a schoolboy error somewhere!!
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Date switching US to UK

Post by Mark Baker »

How is the data stored in MySQL? In a date type column or in a VARCHAR2?

Try

Code: Select all

print date('d.m.Y',strtotime($row["date"]));
PlateSpin
Forum Newbie
Posts: 9
Joined: Tue Sep 29, 2009 6:15 am

Re: Date switching US to UK

Post by PlateSpin »

Cheers Mark, your first shot hit the bullseye. I thought I'd tried your suggestion so confidence wasn't high but......... :banghead:

Thanks again!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Date switching US to UK

Post by pickle »

If it's stored as a DATETIME or DATE format, you should do the formatting in your query, rather than parsing it in PHP.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
PlateSpin
Forum Newbie
Posts: 9
Joined: Tue Sep 29, 2009 6:15 am

Re: Date switching US to UK

Post by PlateSpin »

It is stored as a DATE format pickle but surely if it works, it works. Or am I allowing a fundamental flaw to exist in the way that Mark has suggested?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Date switching US to UK

Post by Mark Baker »

PlateSpin wrote:It is stored as a DATE format pickle but surely if it works, it works. Or am I allowing a fundamental flaw to exist in the way that Mark has suggested?
There's no flaw, but the solution I offered is not the only solution to your problem.... and "if it works, it works" isn't necessarily a good maxim.
A push bike and a BMW will both get you from A to B. In that regard, they both work, but differently. If I want to get from A to B in comfort and more quickly, I might pick the BMW. If I want to avoid poluting the planet, and get some exercise, while going from A to B, I might choose the push bike.

We don't always povide the best solution for your circumstances unless we know more about those circumstances. That's why my initial response not only offered the "universal solution" but also what datatype you were using to store the dates in your database
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Date switching US to UK

Post by pickle »

Certainly doing it in PHP isn't bad, but in my opinion (important to note it's my opinion) it's not the best.

If I've got a date field I know I'm going to be using for a particular purpose, I usually do the date formatting in the query. It's more efficient clock-cycle-wise to do that sort of formatting in MySQL than PHP.

Look up the DATE_FORMAT() MySQL function.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
PlateSpin
Forum Newbie
Posts: 9
Joined: Tue Sep 29, 2009 6:15 am

Re: Date switching US to UK

Post by PlateSpin »

Okay guys I hear what you're saying, thanks for the advice it's greatly appreciated. So I'll away and look up DATE_FORMAT, or should that be I'll get off this bike and go seek out a BMW :)
Post Reply