使用转换后的 Unix 时间戳来查询数据库

发布于 2024-11-01 03:25:17 字数 2022 浏览 6 评论 0原文

我目前正在使用这个:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涙—继续流 2024-11-08 03:25:17

您插入的时间戳需要作为字符串引用,因此您需要

WHERE 
    (
        UNIX_TIMESTAMP('$current_local_time') BETWEEN 
            UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open)) 
        AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close)) 
    ) 

使用可以进行参数绑定的 SQL 框架,如下所示指定查询,并绑定 ? PHP 中的 $current_local_time

WHERE 
    (
        UNIX_TIMESTAMP(?) BETWEEN 
            UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open)) 
        AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close)) 
    ) 

编辑:我认为您想要

(UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open))     <=   '$timestamp2'
AND
'$timestamp2'    <    UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close)))
--              ^^^ Note the less-than sign

您可能不再需要 $timestamp2 周围的引号。

The timestamp that you're inserting needs to be quoted as a string, so you need

WHERE 
    (
        UNIX_TIMESTAMP('$current_local_time') BETWEEN 
            UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open)) 
        AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close)) 
    ) 

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.

WHERE 
    (
        UNIX_TIMESTAMP(?) BETWEEN 
            UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open)) 
        AND UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close)) 
    ) 

EDIT: I think you want

(UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.open))     <=   '$timestamp2'
AND
'$timestamp2'    <    UNIX_TIMESTAMP(CONCAT(CURRENT_DATE(), ' ', t.close)))
--              ^^^ Note the less-than sign

You probably don't need quotes around $timestamp2 anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文