简体   繁体   中英

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.

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!

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'))."'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM