在PHP中获取特定的日期/时间值
问题描述:
I have a cron task that logs a value in my database every 15 minutes so my database rows look something like this:
1, 100, 1311312600
1, 100, 1311313200
1, 90, 1311313800
etc.
The third field is a time() value.
Lets say the current time is 1:00PM on 07/22/2011. I want to see all of yesterdays values, so I need the following times for my SQL query.
07/22/2011 00:00:00
07/21/2011 00:00:00
How do I generate those values from the current time? Keep getting tripped up!
我有一个cron任务,每隔15分钟在我的数据库中记录一次值,所以我的数据库行看起来像这样: p>
1,100,1311312600
1,100,1311313200
1,90,1311313800
code> pre>
等。 p>
第三个字段是time()值。 p>
假设当前时间是2011年7月22日下午1:00。 我希望看到所有昨天的值,所以我需要以下时间进行SQL查询。 p>
07/22/2011 00:00:00
07 / 21/2011 00:00:00
code> pre>
如何从当前时间生成这些值? 继续绊倒! p>
div>
答
$yesterday_begin = strtotime('-1 day 12:00:00 AM');
$yesterday_end = strtotime('-1 day 11:59:59 PM');
Then
SELECT * FROM ... WHERE time_stamp BETWEEN $yesterday_begin AND $yesterday_end
答
$start = mktime(0, 0, 0, date('n') - 1, $day, date('Y');
$end = mktime(0, 0, 0, date('n'), $day, date('Y');
答
select *
from logtable
where DATE(FROM_UNIXTIME(timestampfield)) = '2011/07/21'