PHP check if date is 30 days in the past
14,421
Solution 1
if ($date < strtotime('-30 days'))
If you are only performing actions on dates older than 30 days, you should use Marco's solution.
Solution 2
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)
Comments
-
Aart den Braber almost 2 years
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. :(
-
Marco over 12 yearsPHP solution is correct and perhaps OP needed that. +1 for you! :)
-
Brad over 12 years+1 for yours as well... he wasn't quite clear on what he needed. :-D For all I know, he could have wanted something exactly 30 days ago, but I can't think of a time when anyone has needed that.
-
Aart den Braber over 12 yearsI'm also executing a code for when it's not 30 days ago. I need this solution for an other project though, so thanks!
-
Aart den Braber over 12 yearsOk... I now need this code, but it definitely does not work.. :/ I'm using the
time()
format, which might cause this (although I think it shouldn't). It just always returns positive, whatever I do. -
Marco over 12 years@Aart: I don't understand where you're using
time()
; my query return s records where date in database is between current date/time and 30 days ago... so it's using days... if you use only time it cannot work... Let me understand better... -
Aart den Braber over 12 yearsWell, I'm inserting into the database:
INSERT INTO applicants (date_last_applied) VALUES ('" . time() . "')
. Now I'm retrieving that date, using$result = mysql_query("SELECT * FROM DDFS_applicants WHERE date_last_applied < DATE_SUB(NOW(), INTERVAL 1 DAY)"); while ($row = mysql_fetch_array($result)) echo $row['id_member'];
but it's not working. :( It always returns positive. Same with your other code (BETWEEN). -
Aart den Braber over 12 years'DDFS_applicants' should have been 'applicants', but that isn't the fault in my code. Also: with "returns positive", I mean that it gives the id_member of every entry.
-
Marco over 12 years@Aart: I think the error is because you're saving only time in your database, so how system can check date too
(INTERVAL 1 DAY)
? -
Aart den Braber over 12 yearsI don't quite get what you mean.
time()
is years, months, dayes and so on, right? But do you have a suggestion on how to make it work? -
Alvaro Castro almost 4 yearsFor this solution to work,
$date
has to be a timestamp. If your date is not a timestamp, use thisstrtotime($date) < strtotime('-30 days')