测距日期中的SQL WHERE子句计算

I Have project in which a module needs to display the list of data using a group of specific criteria:

Today
Week
Month
Year

The Database table contains a date field which has datatype as BIGINT(10) and through PHP code we insert time() function value into it. which insert some value like 1311144077,

Now I need to fetch the records from my table as per the labels mentionedabove, how can i do that?

While you're storing datetime in epoch timestamp format, I think solutions using MySQL date/time functions could be very slow. So, I suggest using timestamp itself like below codes.

Usage example

$t = time();

# today
echo date('Y-m-d H:i:s', strtotime('today', $t))."
";
echo date('Y-m-d H:i:s', strtotime('tomorrow', $t))."
";

# this week  
echo date('Y-m-d H:i:s', strtotime('-1 sunday', $t))."
";
echo date('Y-m-d H:i:s', strtotime('sunday', $t))."
";

# this month  
echo date('Y-m-d H:i:s', strtotime(date('Y-m-01 00:00:00', $t)))."
";
echo date('Y-m-d H:i:s', strtotime(date('Y-m-01 00:00:00', strtotime('next month', $t))))."
";

# this year
echo date('Y-m-d H:i:s', strtotime(date('Y-01-01 00:00:00', $t)))."
";
echo date('Y-m-d H:i:s', strtotime(date('Y-01-01 00:00:00', strtotime('next year', $t))))."
";

In query

# this year
$start_of_year = strtotime(date('Y-01-01 00:00:00', $t));
$end_of_year = strtotime(date('Y-01-01 00:00:00', strtotime('next year', $t)));
$query = "SELECT * FROM sometable WHERE somecolumn >= $start_of_year AND somecolumn < $end_of_year";

You can get lower bound timestamp with strtotime() function, eg. strtotime('-1 week'), and then query your database for all records with date field's value greater than that.

Eg. to get a rows that are a week old or newer, you can do:

$ts = strtotime('-1 week');
// or $ts = strtotime('date'); where date can be a date in Y-m-d format
mysql_query("SELECT * FROM table WHERE your_bigint_column >= $ts");

If you need to group by month, you can just use MySQL functions for processing dates:

SELECT WEEK(FROM_UNIXTIME(your_bigint_column)) AS no_of_week, count(*)
FROM table
GROUP BY WEEK(FROM_UNIXTIME(your_bigint_column))

On an unrelated note - BIGINT(10) is an overkill, time()'s result can be safely stored in an INT column.

this command will change the timestamp saved in database date("j F,Y", "1222041600") as 1-6-1987 then you will be having month day and year and on this you can calculate week too. See php functions of date for more details.