Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get DATETIME in php and post it to MySQL for transaction consistency

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

So to ensure a single transaction with any number of queries (20+ queries for example) has a 100% accurate and consistent NOW() value accross multiple tables, what is the php way to assign a variable equivalent of DATETIME using NOW() (not current TIMESTAMP).

like image 944
Maverick Avatar asked Apr 12 '12 18:04

Maverick


1 Answers

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 issues of timezones, or local date formats.

like image 200
Ariel Avatar answered Sep 28 '22 18:09

Ariel