Forum Moderators: coopster

Message Too Old, No Replies

Using Date's PHP

Use PHP to check specified dates against current date

         

rwilson

5:08 am on Dec 24, 2010 (gmt 0)

10+ Year Member



I have records that will have expiration dates and I want them to stop showing up when they reach it. I imagine somehow in my query I would say something like:

WHERE date >= current date

Any help on the writing the correct syntax for this would be greatly appreciated!

edit: the title on this is a little crazy, sorry it's late. I'd change it if it would let me.

Frank_Rizzo

10:14 am on Dec 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This depends on the current format of the dates. Are they stored in a database? What format are they?

Database format could be

2010-12-24 (mysql date)
2010-12-24 09:59:00 (mysql date-time)
1293184922 (unix timestamp)

In order to compare that date with the current date you need to make the formats similar.

In PHP you can convert the current date to the required format:

echo date("Y-m-d"); // displays 2010-12-24
echo date("Y-m-d h:m:s"); // displays 2010-12-24 09:59:00
echo time(); // displays 1293184922

So work out what your date to test format is and compare with the comparable php code.


<?php
$expires = '2010-12-24'; // date pulled from the database
if($expires < date("Y-m-d")) {
echo 'You are authorised to view this content';
} else if($expires == date("Y-m-d")) {
echo 'Your subscription expires today. You can view the content but please renew your subscription';
} else {
echo 'Sorry. Your subscription has expired. You are not allowed to view this page.';
}
?>


If your database format is different then you just need to modify the php code to match it.