tags:

views:

65

answers:

2

What I'm trying to do is automatically change the output format of dates in my sql queries without having to specify it (PHP/Oracle).

I'm using to_char(pi.date_of_birth , 'YYYY/MM/DD') as date_of_birth to change the formatting of my queries. What I'd like to be able to do is apply this to any date returned by the '*' operator.

My problem is that I can't write all of my queries by hand, most of them are generated dynamically, and it would be much easier to change the format globally some way.

Now the problem I also have is that I have 'zero' control over the database. So I can't go in and change anything, or add any procedures. Is there some syntax I can append to my queries, or a simple way of integrating it into php? Thanks.

+1  A: 

Why do you need to format the output in the database layer at all? Why not retrieve it as whatever type PHP uses for dates and times, and format it there? Likewise for input values, you should use parameterised queries and specify the values as dates/times rather than putting them into the SQL in a particular format.

Jon Skeet
I understand at what you're getting at, but I don't quite get how it's possible. When I run a query in php, in the dataset that's returned all the values of type string. Even the strictly numerical values are. It doesn't seem to matter what type they're are stored as in the database, when I run the query they are returned as strings.
The_Denominater
@The_Denominator: I don't know enough about PHP to help any more unfortunately - but I *really hope* there's a way of getting properly typed data out of the database. I find it hard to believe that PHP would be so fundamentally lacking.
Jon Skeet
Yeah, I assume that it's entirely possible, it would be strange if it were not. I just don't know how. Thanks for your help.
The_Denominater
So if I'm reading the documentation correctly it seems that the oci8 driver I'm using has that particular flaw.http://www.php.net/manual/en/oci8.datatypes.php
The_Denominater
@The_Denominator: Blech. You have my sympathies. I'll leave this answer up for anyone coming up against the same thing but with a better driver - but I don't think I can really help you :(
Jon Skeet
A: 

if you really need to change the format, modify your connection script to add an alter session that will change the NLS_DATE_FORMAT

$db = new PDO("oci:dbname=" . $dbSource, $dbUser, $dbPass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "ALTER SESSION SET NLS_DATE_FORMAT='YYYY/MM/DD'";
$db->exec($sql);
Mark Baker
Thanks, I'm using the oci8 abstraction layer at the moment, I can't seem to find the equivalent function, do you by any chance know what it would be?
The_Denominater
Don't know if this link helps: http://www.issociate.de/board/post/192412/OCI_ignoring_NLS_DATE_FORMAT_parameter.html ... seems some people have problems with setting NLS_DATE_FORMAT, but Chris Jones' answer may be of some use
Mark Baker