Welcome Guest, Not a member yet? Register   Sign In
Using NOW() with Active Record
#1

[eluser]Morty[/eluser]
Hi there!

As I said in the topic, I want to send the following table into my DB:
Code:
if ($this->input->post('cosm') == 0)
            $data['cosm'] = 'N/A';
else
            $data['cosm'] = $this->input->post('cosm');
$bc1 = explode(" - ", $this->input->post('bc1'));
$data['bc1'] = $bc1[0];
$bc2 = explode(" - ", $this->input->post('bc2'));
$data['bc2'] = $bc2[0];
$data['acc'] = $this->input->post('acc');
$data['comment'] = $this->input->post('comment');

// My issue is HERE
$data['recorddate'] = 'NOW()';
$this->db->insert('doa', $data);

Of course I know that my code is WRONG, but that is only for illustration purposes.

Thanks in advance.
#2

[eluser]sophistry[/eluser]
does this work?
Code:
$data['recorddate'] = date("Y-m-d H:i:s");
#3

[eluser]Morty[/eluser]
Hopefully in my case it works, since SQL and PHP are on the same machine. (No time difference). Thanks, I did not want to use that but it seems to be the only option available.
#4

[eluser]vokic[/eluser]
Hi.. You can mod the DB_driver.php in order to allow SQL statements in active record with something like this:

Code:
function escape($str)
{    
    switch (gettype($str))
    {
        case 'string'    :
            if (preg_match("/^SQL_(.+)/", $str, $matches))
                $str = $this->escape_str($matches[1]);
                else $str = "'".$this->escape_str($str)."'";
            break;

        case 'boolean'    :    $str = ($str === FALSE) ? 0 : 1;
            break;

        default            :    $str = ($str === NULL) ? 'NULL' : $str;
            break;
    }
    return $str;
}

so you can use it like this:

Code:
$data['date'] = 'SQL_NOW()';
or
Code:
$data['date'] = 'SQL_UNIX_TIMESTAMP()';

and you can use MySQL time in queries so there is no time difference...
#5

[eluser]sophistry[/eluser]
oh.... you need some kind of timezone adjustment... because of two separate machines...

do you know where the machines are located?
can you adjust the server timestamps based on the timezone of the PHP request?
or is it even more complicated than that?




Theme © iAndrew 2016 - Forum software by © MyBB