Cully
Cully

Reputation: 470

Working with strtotime and MySQL

This works...

 mysqli_query
($con, "
    INSERT INTO myDB (id, name, mydate) 
    VALUES ('$id', '$name', now())
")

This doesn't work....

 mysqli_query
($con, "
    INSERT INTO myDB (id, name, mydate) 
    VALUES ('$id', '$name', date('Y-m-d H:i:s', strtotime('+5 hour')))
")

The mydate column in MySQL is of datetime format.

Any ideas why it's not working? I'm currently using now(), but I want it to show the time in my timezone, not the server's timezone.

Upvotes: 0

Views: 4913

Answers (2)

Amal
Amal

Reputation: 76646

I'd suggest storing the date in a variable first.

$date = date('Y-m-d H:i:s', strtotime('+5 hour'));

If both your MySQL and PHP servers are operating on the same time-zone and have their clocks properly synchronized, you wont have an issue.

and then execute the query like so:

mysqli_query($con, "
    INSERT INTO myDB (id, name, mydate) 
    VALUES ('$id', '$name', '$date')
")

I hope this helps!

Upvotes: 2

user1646111
user1646111

Reputation:

strtotime() is not MySQL function, its PHP function, you need to write it for being executes...

    "INSERT INTO myDB (id, name, mydate) 
    VALUES ('$id', '$name', '".date('Y-m-d H:i:s', strtotime('+5 hour'))."'

Upvotes: 1

Related Questions