My brain won't work (in fact it doesn't often when it comes to comparing dates). I need to check whether a date returned from the database is 7 days or less from the current date (can't do it in database as it is used to highlight events where entries close within 7 days or less).

What I have for now (which isn't working quite as expected) is:

$closing_date = strtotime('-7 day', strtotime($show['closing_date']));
$current_date = strtotime('Y-m-d');
if ($closing_date > $current_date) { .... }

It's OK, I have it sorted (as usually happens after I post on here) and for one I had missed the date() parameter, but if anyone is interested, I now have

$closing_date = strtotime('-7 day', strtotime($show['closing_date']));
$current_date = strtotime('+7 day', strtotime(date('Y-m-d')));
if ($closing_date < $current_date) { ..... } ?>

Because I need all events shown and then highlight the closing date if it is less than 7 days so I can't just query for events that close in less than 7 days.

Member Avatar

You can use a 'calculated field' as a flag.


SELECT *, DATEDIFF(NOW(), `date_field`) AS diff FROM table...

Should give you all your fields with the added 'diff' field which should contain an integer (difference in days). BTW - dates confuse the hell out of me too, so you may need to swap the two dates around - depends on which way you need the relevant data - negative or positive integers.

Thanks, I will have a look at that - to be honest I have very rarely used date functions in my queries as have just stuck to doing it in the PHP after years of doind it that way :)

Oh wow - that works brilliantly and so much easier than trying to work it all out in PHP plus hase saved me a few lines of code. Thank you diafol :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.