vinagree
vinagree

Reputation: 73

OPERATION WITH UNIX DATE

Im making a SQL like this:

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

Date2 always is going to be today date PHP time(); (UNIX FORMAT). Now I need take date2 minus 7days ( 1 week) and date2 minus 1 month.

(This will be choosen by the user on a form).

The problem is that i can't get a operation with UNIX dates work.

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

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

HTML FORM

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

Upvotes: 0

Views: 196

Answers (2)

nl-x
nl-x

Reputation: 11832

$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

or more exact would for $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

Upvotes: 1

Mohamed Tarek
Mohamed Tarek

Reputation: 385

What did you try?

$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));

Upvotes: 1

Related Questions