0

我正在制作这样的 SQL:

SELECT * FROM POST
WHERE date between :date1 and :date2

Date2 永远是今天的日期 PHP time(); (UNIX 格式)。现在我需要 date2 减去 7 天(1 周)和 date2 减去 1 个月。

(这将由用户在表单上选择)。

问题是我无法使用 UNIX 日期进行操作。

$date2 = time();
$date1 = $_GET['fromDate'];

$query = "SELECT * FROM POST
          WHERE date between :date1 and :date2";

HTML 表格

 select id=fromDate>
    <option value=<?php echo time()-604800>WEEK</option>
/select>
4

2 回答 2

1
$today = time();
$oneWeekAgo = time() - (60 * 60 * 24 * 7); // 60 seconds, 60 minutes, 24 hours, 7 days
$oneMonthAgo = time() - (60 * 60 * 24 * 30); // 60 seconds, 60 minutes, 24 hours, 30 days

或更准确地说是 $oneMonthAgo:

$date = date_create(); // get DateTime object of today
date_modify($date, "-1 month"); // 1 month ago
$oneMonthAgo = date_timestamp_get($date); // get unix time
于 2013-04-09T08:28:09.250 回答
1

你尝试了什么?

$t=time();
# this would output current date
echo(date("D F d Y",$t));
# minus 7 days converted to seconds
$t -= 7*24*60*60;
# this would print the original $t minus 7 days
echo(date("D F d Y",$t));
于 2013-04-09T08:28:22.223 回答