I want to display a 'NEW' div when an image has a 'date created' value set in a database, that is no more than a week old.
$now = new DateTime();
$then = new DateTime($imageSet->created);
$diff = date_diff($now,$then);
if($diff->format('%d') >= 0 && $diff->format('%d') < 8 && $imageSet->created != null) { $new = 1; } else { $new = 0; }
But with an image that has a date created value of 2016-05-17, this is showing the div 'NEW', when it's obviously much more than a week old. Based on my condition above, I'm expecting any date created value of 2016-07-12 or later to trigger the 'NEW' div.
Any ideas why it's not?
%d
is the days part of a "years, months, days" description. In the case of a 40 day total difference, say, it would be around 9 days, because it's a one month, nine days difference.
If you want the total day difference, use %a
.
Use following code
$now = new DateTime();
if(empty($imageSet->created)) {
$new = 0
} else {
$then = new DateTime($imageSet->created);
$diff = date_diff($now,$then);
if($diff->format('%a') >= 0 && $diff->format('%a') < 8 ) {
$new = 1;
} else {
$new = 0;
}
}