Question

I want to use PHP and MySQL to INSERT the the day of Yesterday. So my idea was:

INTO chartValues SET timestamp='1353369600', `datetime`=DATEADD(d,-1,GETDATE())

But its not working:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO chartValues SET timestamp='1353369600', datetime=DATEADD(d,-1,GETDATE())' at line 1

Thanks in advance

Was it helpful?

Solution

DATEADD and GETDATE() are T-SQL functions used in SQL Server.

You want to use DATE_ADD() or DATE_SUB() with NOW()

INSERT INTO chartValues SET timestamp='1353369600', `datetime`= DATE_SUB(NOW(), INTERVAL 1 DAY)

Reference

DATE_SUB(date, INTERVAL expr unit)

OTHER TIPS

I believe you should use NOW() in MySql, instead of getdate(). See Mysql Date and Time Functions.

Date_add() not dateadd() now() not getdate()

the link at the bottom is a good reference to mysql programming I recommend it.

so datetime = date_add(d,-1,now())

http://www.w3schools.com/sql/func_date_add.asp

You can use something like this in php:

date("F j, Y", time() - 60 * 60 * 24);

depending the datatype in your database you can change the "F j, Y" with the format that you need.

An in mysql something like this:

CAST(NOW() - INTERVAL 1 DAY AS DATE). 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top