Fetching Results with datetime
I'm trying to fetch results between two time intervals. I've done this before with another web app I bui开发者_开发百科lt using just Y-m-d
, but now having problems with Y-m-d H:i:s
. The code is below (masked of-course for security reasons):
$m5b = date('Y-m-d H:i:s',mktime(0,0,0,0,0,0,date('Y'),date('m'),date('d'),date('H'),date('i')-5,date('s')));
$npm = date('Y-m-d H:i:s',mktime(0,0,0,0,0,0,date('Y'),date('m'),date('d'),date('H'),date('i')+1,date('s')));
$gm = mysql_query("SELECT * FROM acm WHERE lgt !='0000-00-00 00:00:00' AND logged BETWEEN '$m5b' AND '$npm'");
I would seriously recommend reading up on PHP's strtotime()
function and if you're using a recent version of PHP5 then the DateTime object. This should clean up your code no end.
With strtotime()
$t = strtotime('+5 mins');
$mysqlFormatted = date('Y-m-d H:i:s', $t);
With the DateTime class
$t = new DateTime();
$t->modify('+5 mins');
$mysqlFormatted = $t->format('Y-m-d H:i:s');
seems to me that you are passing way too many parameters to mktime() you are passing 12 params?
Edit: Using the code from oblig's answer it actually should be:
$m5b = date("Y-m-d H:i:s" , (time() - (5*60)) ); // 5 mins in seconds
$npm = date("Y-m-d H:i:s" , (time() + (1*60)) );
mktime according to php.net:
int mktime (
[ int $hour = date("H")
[, int $minute = date("i")
[, int $second = date("s")
[, int $month = date("n")
[, int $day = date("j")
[, int $year = date("Y")
[, int $is_dst = -1 ]]]]]]] )
精彩评论