简体   繁体   中英

How to adjust timestamp within sql query?

I have a query:

$time = time();
$query = "SELECT timestamp FROM sales WHERE timestamp < '$time'";

=

The timestamp (in seconds, ie 1554901254) in that DB is 4 hours ahead of time() . Is there a way to adjust that timestamp within the query? I know something like $time = time()+14400 should work, but can I adjust timestamp itself to let's say date_default_timezone_set('America/New_York'); ?

You can do something like that,

 where timestamp < date_add($time, INTERVAL 4 hour)

Documentation for date_add .

date_default_timezone_set('America/New_York');

$datetime = new DateTime();
$datetime->setTimestamp($yourTimestamp);
echo $datetime->getTimezone()->getName();
echo $datetime->format(DATE_ATOM);

This should do the trick for you.

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