I have two dates. One of them is the current date, one of them is a date somebody uploaded something, this is stored in a database. I need to find out if the date stored in the database is older than 7 days from the current date. I'm using PHP's date(d/m/y);
, I've tried some things online, I've tried dateDifference() from php.net, I've tried converting them to timestamps and taking them away, but neither of these seem to work. Is there a simpler way?
Asked
Active
Viewed 41 times
-1

J. Doe
- 13
- 2
-
Please revise your question with a verifiable problem, showing the code you used. Right now we have to guess at the end result you want and have no context on how your dates are stored in the database. For all intents and purposes, this can also be performed in mysql using See: [`DATEDIFF(NOW(), date_column)`](http://sqlfiddle.com/#!9/ed654b/1) – Will B. Mar 16 '18 at 23:32
2 Answers
0
This is what carbon is made for. Consider it.
//time in db is in this format 2018-03-16 08:31:09 for this example
$dateInDb = Carbon::createFromFormat("Y-m-d H:i:s",$timeInDb);
$days = Carbon::now()->diffInDays($dateInDb);

Toby Okeke
- 624
- 3
- 13
-
4No need of Carbon to check the difference between two dates. See @Jeto comment. – Syscall Mar 16 '18 at 23:29
0
try this:
<?php
$upload_date = '09/03/2018'; # d/m/Y format
if (strtotime(date_format(date_create_from_format('d/m/Y',$upload_date),'Y-m-d')) < strtotime('-7 days')) {
echo 'Upload date is older than 7 days ago.';
} else {
echo 'Upload date is not older than 7 days ago.';
}

Taufik Nur Rahmanda
- 1,862
- 2
- 20
- 36