使用转换后的 Unix 时间戳来查询数据库
我目前正在使用这个:
$rawsql = "SELECT
*
FROM
_erc_foffices n
INNER JOIN
_erc_openings o ON n.id = o.branch_id AND o.dotw = DAYOFWEEK(CURRENT_DATE())
INNER JOIN
_erc_openings_times t ON o.id = t.opening_id
WHERE
(
UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) BETWEEN
UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open))
AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close))
)
AND
(
n.id = %d
)
;";
查询数据库,并返回大使馆的所有详细信息(如果当前处于打开状态)。
现在,我需要确定变量 $current_local_time
中存储的时间是否在两个数据库值之间,而不是 CURRENT_TIMESTAMP()
。时间表时间需要存储在当地时间而不是伦敦时间,所以这就是我尝试转换它们的原因。
我尝试用替换
WHERE
(
UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) BETWEEN
UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open))
AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close))
)
,
WHERE
(
UNIX_TIMESTAMP($current_local_time) BETWEEN
UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open))
AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close))
)
但我只收到错误消息。有人可以告诉我如何正确地做到这一点吗?
感谢您的帮助
编辑:$current_local_time
变量的获取方式如下:
date_default_timezone_set('Europe/London');
$time = date("H:i", time());
$myDateTime = new DateTime($time, new DateTimeZone('GMT'));
$myDateTime->setTimezone(new DateTimeZone($timezone));
$current_local_time = $myDateTime->format('H:i:s');
并输出为回显时另一个国家/地区的当前时间。
编辑 2:
我已将 $current_local_time 更改为:
$current_local_time = $myDateTime->format('H:i:s');
$timestamp2 = strtotime($current_local_time);
并将 where 子句更改为:
WHERE
(
'$timestamp2' BETWEEN
UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open))
AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close))
)
这工作正常,但如果 t.close
设置为 23:30,则实际上要到 23 才会关闭:31。有没有办法从捕获的时间中抽出一分钟来解决这个问题?
I am currently using this:
$rawsql = "SELECT
*
FROM
_erc_foffices n
INNER JOIN
_erc_openings o ON n.id = o.branch_id AND o.dotw = DAYOFWEEK(CURRENT_DATE())
INNER JOIN
_erc_openings_times t ON o.id = t.opening_id
WHERE
(
UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) BETWEEN
UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open))
AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close))
)
AND
(
n.id = %d
)
;";
to query the database, and return all of the details from the embassy if it is currently open.
Now, rather than the CURRENT_TIMESTAMP()
, I need to find out whether the time stored in variable $current_local_time
is between the 2 database values. Timetable times need to be stored in local times instead of London's times, so that's why I'm trying to convert them.
I have tried replacing the
WHERE
(
UNIX_TIMESTAMP(CURRENT_TIMESTAMP()) BETWEEN
UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open))
AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close))
)
with
WHERE
(
UNIX_TIMESTAMP($current_local_time) BETWEEN
UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open))
AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close))
)
but I just get error messages. Could someone please tell me how I would do this properly?
Thanks for any help
Edit: The $current_local_time
variable is taken like so:
date_default_timezone_set('Europe/London');
$time = date("H:i", time());
$myDateTime = new DateTime($time, new DateTimeZone('GMT'));
$myDateTime->setTimezone(new DateTimeZone($timezone));
$current_local_time = $myDateTime->format('H:i:s');
and outputs as whatever the current time in the other country is when echoed.
Edit 2:
I have changed the $current_local_time to this:
$current_local_time = $myDateTime->format('H:i:s');
$timestamp2 = strtotime($current_local_time);
and the where clause to this:
WHERE
(
'$timestamp2' BETWEEN
UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open))
AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close))
)
This works OK, but if t.close
is set at 23:30, it won't actually shut until 23:31. Is there a way to take a minute off of the captured time to sort this out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您插入的时间戳需要作为字符串引用,因此您需要
使用可以进行参数绑定的 SQL 框架,如下所示指定查询,并绑定
?
PHP 中的$current_local_time
。编辑:我认为您想要
您可能不再需要
$timestamp2
周围的引号。The timestamp that you're inserting needs to be quoted as a string, so you need
It's even better to use a SQL framework that lets you do parameter binding, specify the query as follows, and bind the
?
to$current_local_time
in PHP.EDIT: I think you want
You probably don't need quotes around
$timestamp2
anymore.