Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP check if date is 30 days in the past

Tags:

date

sql

php

back

I'm having a bit of a problem here.

I insert a date into the database: date_last_applied.

I can just call this by using $row['date_last_applied'], of course. Now, I need to check if this inserted date was 30 days ago and if so, execute an action.

$query = "SELECT date_last_applied FROM applicants WHERE memberID='$id'";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)) {
    $date = strtotime($row['date_last_applied']);

}

That's about all I have.. I tried some things, but they all failed. :(

like image 932
Aart den Braber Avatar asked Nov 10 '11 14:11

Aart den Braber


2 Answers

if ($date < strtotime('-30 days'))

If you are only performing actions on dates older than 30 days, you should use Marco's solution.

like image 137
Brad Avatar answered Sep 22 '22 15:09

Brad


You could do it via SQL getting only dates in last 30 days

SELECT date_last_applied 
FROM applicants 
WHERE memberID = your_id
  AND date_last_applied BETWEEN
      DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()

or older than 30 days

SELECT date_last_applied 
FROM applicants 
WHERE memberID = your_id
  AND date_last_applied < DATE_SUB(NOW(), INTERVAL 30 DAY)
like image 30
Marco Avatar answered Sep 22 '22 15:09

Marco