开发者

today and not last 24 hours?

开发者 https://www.devze.com 2023-02-20 17:44 出处:网络
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. thus it is no longer 86400 seconds, it should be after 12

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. thus it is no longer 86400 seconds, it should be after 12 midnight till current time.

hope this makes sens开发者_StackOverflow社区e.. 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:

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


time()-strtotime('today') - difference between now and midnight; time()-strtotime('yesterday') - difference between now and yesterday midnight; time()-strtotime('-2 days')...

for yesterday only (range $min to $max)

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

etc.


Following will give you the seconds passed since January 1, 1970. Every object with a timestamp higher than this value is from the current day (given that you have set your timezones and local time correctly).

$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. For more info, see: http://php.net/manual/en/function.date.php and http://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');

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.

So:

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

精彩评论

暂无评论...
验证码 换一张
取 消