开发者

When insert current time to db, which way is better?

开发者 https://www.devze.com 2023-01-04 09:03 出处:网络
I want to update the update_date field in oracle db. I have two ways to do it. Which one is the better?开发者_Python百科

I want to update the update_date field in oracle db.

I have two ways to do it. Which one is the better?

开发者_Python百科

1.$now = date('Y-m-d H:i:s');

2.$now = "TO_CHAR(CURRENT_TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS')";


It depends if you want to rely on the time of the php-server or of the oracle-server.


On version #1, PHP is responsible for the date formatting.

On version #2, this is Oracle

It's almost the same, provided that Y-m-d H:i:s is equivalent to YYYY-MM-DD HH24:MI:SS

If you are always setting the current date, I would say that you can do that on the DB side : it will save at least one concatenation.

$sql = 'UPDATE foo SET date = TO_CHAR(CURRENT_TIMESTAMP, "YYYY-MM-DD HH24:MI:SS")';
// VERSUS
$sql = 'UPDATE foo SET date = "' . date('Y-m-d H:i:s') . '"';


In your second solution (2), you assume that 1) you database engine time is correct (correct timezone and so on), and 2) that you are using Oracle forever (otherwise, the SQL query might change).

If you use php (1), then you can make sure of that the time issues are solved from the code (performing check on the timezone etc...). Moreover, the query doesn't change even if you swtich ot another DB engine.

0

精彩评论

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