简体   繁体   English

今天而不是过去的24小时?

[英]today and not last 24 hours?

i my php codes i do time()-86400 to fetch everything from the last 24 hours, but how i can get everything today or everything from yesterday. 我我的php代码,我做time()-86400来获取过去24小时中的所有内容,但是我如何才能得到今天或昨天的所有内容。 thus it is no longer 86400 seconds, it should be after 12 midnight till current time. 因此,它不再是86400秒,而是应该在午夜12点之后到当前时间。

hope this makes sense.. but how i can do this? 希望这是有道理的..但是我该怎么做呢?

If you are "fetching" from a database, why not do it in the query? 如果要从数据库中“获取”,为什么不在查询中这样做呢?

SELECT * FROM `table` WHERE DATE(`created_at`) = '2011-03-28';

If you are storing the date as a unix timestamp: 如果要将日期存储为UNIX时间戳记:

SELECT * FROM `table` WHERE DATE(FROM_UNIXTIME(`created_at`)) = '2011-03-28';

time()-strtotime('today') - difference between now and midnight; time()-strtotime('today') -现在和午夜之间的时差; time()-strtotime('yesterday') - difference between now and yesterday midnight; time()-strtotime('yesterday') -现在和昨天午夜之间的时差; time()-strtotime('-2 days') ... time()-strtotime('-2 days') ...

for yesterday only (range $min to $max) 仅适用于昨天(范围从$ min到$ max)

$start = strtotime('yesterday')
$end = strtotime('today') - 1;

etc. 等等

以下将为您提供自1970年1月1日以来经过的秒数。每个时间戳高于此值的对象都是从当天开始的(假设您已正确设置了时区和本地时间)。

$time = strtotime(date('Y-m-d 00:00:00'));

You can use the PHP date and strtotime function in order to pick a day from now and retrieve the seconds that specific date. 您可以使用PHP datestrtotime函数来从现在开始选择一天并检索该特定日期的秒数。 For more info, see: http://php.net/manual/en/function.date.php and http://php.net/manual/en/function.strtotime.php 有关更多信息,请参见: http : //php.net/manual/en/function.date.phphttp://php.net/manual/en/function.strtotime.php

I agree with Gordon here - there are so many date/time examples. 我在这里同意戈登的观点-有很多日期/时间示例。 But hey, let's go over it again - assuming today begins at midnight, you use: 但是,让我们再看一遍-假设今天从午夜开始,您可以使用:

$start = strtotime('today'); $ start = strtotime('today');

Assuming "today" ends at 23:59, simple arithmetics imply that if you increment the $start by 24 hours and take away 1 second - you'll reach the end of today. 假设“今天”在23:59结束,那么简单的算术就意味着,如果您将$ start增加24小时并花费1秒钟,那么您将到达今天的结尾。

So: 所以:

$start = strtotime('today');
$end = $start + (3600 * 24) - 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM