开发者

Automatically format sql dates

开发者 https://www.devze.com 2023-01-10 10:08 出处:网络
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).

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 开发者_StackOverflow中文版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.


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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消