简体   繁体   English

服务器/本地主机之间的不同环境正在更改日期格式-PHP PDO MySQL / oci

[英]Different environment between server/localhost is changing the date format - php pdo mysql/oci

I'm having problems to configure my server environment, the PHP with PDO is not formatting the date like the localhost. 我在配置服务器环境时遇到问题,带有PDO的PHP无法像本地主机一样格式化日期。 To test it, I created two connections (using pdo with oci and mysql). 为了测试它,我创建了两个连接(在oci和mysql中使用pdo)。 On localhost, oci and mysql runs normally, but on server only the mysql maintains the correct format. 在localhost上,oci和mysql正常运行,但在服务器上只有mysql保持正确的格式。

IMPORTANT DETAIL: On sqldeveloper, it shows the data in the same format like localhost pdo/oci. 重要说明:在sqldeveloper上,它以与localhost pdo / oci类似的格式显示数据。

MY LOCALHOST IS WINDOWS 7 AND MY SERVER IS LINUX DEBIAN X64. 我的本地主机是WINDOWS 7,我的服务器是LINUX DEBIAN X64。

What could to be happening with pdo/oci on server? 服务器上的pdo / oci会发生什么?

Code: 码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sem título</title>
</head>

<body>
<?php
try
{
    $params->host = "172.0.0.0:1521";
    $params->dbname = "geo";
    $params->user = "root";
    $params->pass = "";

    $conn = new PDO("oci:dbname=//$params->host/$params->dbname;charset=UTF8", "$params->user", "$params->pass");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $conn->prepare("select * from tb_geooficio where tipo = 1 and cadastro_im = 37693500 ");
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    print_r($result);
}
catch(Exception $e)
{
    echo $e->getMessage();
}
?>

<?php
try
{
    $host = "200.0.0.1";
    $user = "postmaster";
    $pass = "^PostM@ster^";
    $db = "bd_controleinternet";

    $conn = new PDO("mysql:host=$host; dbname=$db", "$user", "$pass");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $conn->prepare("select * from tbl_secretaria where sec_id = 7");
    $stmt->execute();
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    print_r($result);
}
catch(Exception $e)
{
    echo $e->getMessage();
}
?>
</body>
</html>

Result: 结果:

在此处输入图片说明

In Oracle, you can use the following to change the default format used for date conversion to match the MySQL default: 在Oracle中,可以使用以下命令更改用于日期转换的默认格式以匹配MySQL的默认格式:

ALTER SESSION SET NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss';

This will make the Oracle dates output like MySQL dates always do. 这将使Oracle日期输出始终像MySQL日期一样。

You can make this happen on every connect with PDO: 您可以在每次使用PDO进行连接时使这种情况发生:

$driver_options = array( PDO::MYSQL_ATTR_INIT_COMMAND => 'ALTER SESSION...' );
try {
    $dbh = new PDO($dsn, $user, $pw, $driver_options);
}
catch (PDOException $e) {
    // Handle exception
}

Both Oracle and MySQL also have functions to format dates explicitly, which you can call in expressions in your query select-list. Oracle和MySQL都具有显式格式化日期的功能,您可以在查询选择列表中调用表达式。 But in Oracle the function is TO_CHAR() and in MySQL the function is DATE_FORMAT(), which makes it harder to write RDBMS-independent code. 但是在Oracle中,函数是TO_CHAR(),在MySQL中,函数是DATE_FORMAT(),这使得编写与RDBMS无关的代码变得更加困难。


Re your comment: 发表您的评论:

It seems the NLS_DATE_FORMAT can be set globally in initORCL.ora , it can be set by a LOGON trigger, it can be set at the session level, etc. This could account for the different behavior in two different environments. 看来NLS_DATE_FORMAT可以在initORCL.ora全局设置,可以通过LOGON触发器设置,可以在会话级别设置,等等。这可以解释两种不同环境中的不同行为。 Here's an interesting post about it: 这是一篇有趣的文章:

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:351017764854 http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:351017764854

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM