Page 1 of 1
Date switching US to UK
Posted: Tue Sep 29, 2009 6:42 am
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!!
Re: Date switching US to UK
Posted: Tue Sep 29, 2009 6:54 am
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"]));
Re: Date switching US to UK
Posted: Tue Sep 29, 2009 7:10 am
by PlateSpin
Cheers Mark, your first shot hit the bullseye. I thought I'd tried your suggestion so confidence wasn't high but.........
Thanks again!
Re: Date switching US to UK
Posted: Tue Sep 29, 2009 10:47 am
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.
Re: Date switching US to UK
Posted: Wed Sep 30, 2009 4:19 am
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?
Re: Date switching US to UK
Posted: Wed Sep 30, 2009 7:12 am
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
Re: Date switching US to UK
Posted: Wed Sep 30, 2009 9:42 am
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.
Re: Date switching US to UK
Posted: Thu Oct 01, 2009 4:03 am
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
