Get DATETIME in php and send it to MySQL to negotiate transactions

Here is a link to the reason for this question: NOW () for transaction DATETIME InnoDB guaranteed?

Thus, to ensure a single transaction with any number of queries (e.g. 20 + ) has a 100% accurate and consistent NOW () value across multiple tables, which php way to assign a variable equivalent to DATETIME using NOW () (not current TIMESTAMP) .

+6
source share
3 answers

One of the methods:

<?php $nowFormat = date('Ymd H:i:s'); $sql = "INSERT INTO table SET field='$nowFormat'"; $sql2 = "INSERT INTO table SET field='$nowFormat'"; ... 
+14
source

Do it in MySQL, so you don't use PHP at all:

 select @now := now(); insert into .... values (@now) select from ... where x=@now etc.... 
+4
source

You can do:

 <? $now = time(); $sql = "INSERT INTO table SET field=FROM_UNIXTIME($now)"; $sql2 = "INSERT INTO table SET field=FROM_UNIXTIME($now)"; ... 

This avoids any possible problems with time zones or local date formats.

+2
source

Source: https://habr.com/ru/post/913065/


All Articles