简体   繁体   中英

PHP MYSQL Question on comparing dates in a Query

I am trying to construct a mysql query string to pull out certain records but only if the date in the database is greater than the current date.

So I have this so far and I am not sure if this is a legal syntax...

date_default_timezone_set('America/Los_Angeles');
$current_date = date("Y-m-d");

$sql = "SELECT * FROM `coupons` WHERE status = 1 AND end_date > '$current_date'";

Thanks for your help.

It's legal syntax. You can use one.

I use CURRENT_TIMESTAMP in general, ie:

SELECT * FROM `coupons` WHERE status = 1 AND end_date > CURRENT_TIMESTAMP

Do you have a need to compare to the LA timezone? CURRENT_TIMESTAMP will use the local MySQL server time (but that should technically be what the date values are stored as, as well).

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