tags:

views:

676

answers:

7

I have a DB created by a third party vendor that I'm now writing a new UI for.

The DB stores event start times as unix timestamps (in GMT). What I need to do is query this for a one day range. So presumably I simply need to do a:

SELECT * WHERE start > $last_night_at_midnight AND start < $tonight_at_midnight

The problem I'm running into is a simple way to combine the date/time functions in PHP to create those variables. It feels like everything I'm doing is way too complicated for such a simple procedure.

Does anyone have a simple solution to this?

Cheers

A: 

The time() function in PHP might be of use here: http://be2.php.net/manual/en/function.time.php

More specificaly, the example given on that page will probably tell you what you need to know. Take a look at the 3rd echo statement

Vordreller
+6  A: 

Check out PHP's strtotime() for plain-English to unix time conversion.

This lets you use "today midnight" and "tomorrow midnight" and other similar constructs, as Paolo Bergantino demonstrates.

Nerdling
+5  A: 

strtotime - the awesomenest PHP time function ever.

$last_night_at_midnight = strtotime("today midnight");
$tonight_at_midnight = strtotime("tomorrow midnight");
Paolo Bergantino
midight will give you midnight *today*, which is in the past, rather than the future.
Paul Dixon
If I do a:echo strtotime('10 mar 2009 midnight');The result is 1236657600 which translates to 11pm. Any ideas why this may be happening? DST?
Cory Dee
+3  A: 

strtotime is a useful one here...

$t1=strtotime("today");
$t2=strtotime("tomorrow");

$sql="select * from foo where timecol between $t1 and $t2";

At time of writing (09-03-2009 19:06), today equates to 09-03-2009 00:00:00 and tomorrow equates to 10-03-2009 00:00:00

Paul Dixon
A: 

You could either create those Variables in PHP or in Mysql...

$tonight_at_midnight    = mktime(0,0,0);
$last_night_at_midnight = $tonight_at_mindnight - 86400;

The other possibility is in SQL:


SELECT * FROM tbl1 WHERE start > UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 DAY) 
AND start < UNIX_TIMESTAMP(CURDATE())

(not tested)

dog
+1  A: 

If you need to pick arbitrary days, use gmmktime

Eddy
+4  A: 

This is a really backwards way of doing it, but if you need to be able to do various date ranges, not just today or one day, you could combine the odd time/date features of both languages and go with:

$php_start = strtotime("Some valid date expression");
$php_end = strtotime("Some other valid date expression");

$result = my_sql_query("
          Select * FROM someDB 
          WHERE DATE(FROM_UNIXTIME(date_column)) 
          BETWEEN FROM_UNIXTIME($php_start) AND FROM_UNIXTIME($php_end)");

using the DATE() in the query ensures that the sql won't bother looking at the time part of the data, and using FROM_UNIXTIME for all three means that the sql server is consistent in how it derives the dates.

My favorite way of getting a date range for just today is:

  $today_start = strtotime("today");
  $today_end = strtotime("+1 day", $today_start);

strtotime is actually better, I've found, then adding 86400 or anything like that, because strtotime handles DST better. Found that out last week.

Anthony
+1 for the tip that strtotime() takes DST into account.
pd
If you are doing date/time manipulations or comparisons where the smallest unit is smaller than a day, DO NOT try to code it yourself: use libraries. They already have things like DST bugs sorted out.
staticsan
Using the DATE() function has negated my need to do a 'between x and y' query, instead I just say if date() = date() and it strips off the time just the way I was hoping to.
Cory Dee