如何修改此查询,以便开始/结束日期按天而不是精确时间?

发布于 2024-11-06 09:30:23 字数 577 浏览 1 评论 0原文

我使用下面的查询来获取基于使用 UNIX 时间戳的开始日期和结束日期的帖子。

目前,仅当开始时间晚于今天的时间时才返回结果。例如,如果时间为下午 5:00,开始日期为下午 6:00,则不会选取它。我希望将今天的任何时间(从昨晚午夜到今晚午夜)视为开始日期。

有人可以告诉我如何更新我的代码以使其正常工作吗?

$current_time 等于 1305132894,即今天美国东部时间 5:54。

'meta_query'        => array(
    array(
        'key'       => 'start_date',
        'value'     => $current_time,
        'compare'   => '<'
    ),
    array(
        'key'       => 'end_date',
        'value'     => $current_time,
        'compare'   => '>'
    )
)

I'm using the query below to get posts based on the start and end dates which use the UNIX timestamp.

Currently, it's only returning results if the start time occurred after today's time. For example, if it's 5:00 PM and the start date is 6:00 PM, it won't pick it up. I would like it to consider anytime during today (from midnight last night to midnight tonight) as being the start date.

Can someone tell me how to update my code to get this to work?

$current_time is equal to 1305132894, which is 5:54 EST today.

'meta_query'        => array(
    array(
        'key'       => 'start_date',
        'value'     => $current_time,
        'compare'   => '<'
    ),
    array(
        'key'       => 'end_date',
        'value'     => $current_time,
        'compare'   => '>'
    )
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

寂寞笑我太脆弱 2024-11-13 09:30:23

如果您只需要它今天工作,您可以使用:

$minDate = strtotime('midnight today');
$maxDate = strtotime('midnight tomorrow -1 second');

或者

$minDate = mktime(0,0,0);
$maxDate = mktime(11,59,59);

mktime() 的某种组合strtotime() 应该可以工作。

If you only need it to work for today, you can use:

$minDate = strtotime('midnight today');
$maxDate = strtotime('midnight tomorrow -1 second');

or alternatively

$minDate = mktime(0,0,0);
$maxDate = mktime(11,59,59);

Some combination of mktime() and strtotime() should work.

烟─花易冷 2024-11-13 09:30:23

考虑您的示例: $current_time = 1305132894

这应该可以回答您的问题:

$date_current = date("Y-m-d",1305132894);  //2011-05-11
$date_midnight = strtotime($date_current . "00:00:00"); //1305061200 that means: 2011-05-11 00:00:00

因此,将 $current time 替换为 $date_midnight ,您将在今天从昨晚午夜开始的任何时间。

Considering your example: $current_time = 1305132894

This should answer your question:

$date_current = date("Y-m-d",1305132894);  //2011-05-11
$date_midnight = strtotime($date_current . "00:00:00"); //1305061200 that means: 2011-05-11 00:00:00

So replace $current time by $date_midnight and you'll have anytime during today from midnight last night.

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