Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Convert MySQL date with PHP and check if date has passed.

Posted on 2006-05-28
6
Medium Priority
?
374 Views
Last Modified: 2013-12-12
Hi,
I have an expiry date - MySQL date format 2006-07-14,
can anyone please tell how I can convert this and check if the date has passed or expired with PHP.

Thanks
0
Comment
Question by:sabecs
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:rbartz
ID: 16778873
This should work easy and fast:

$result=mysql_query("SELECT UNIX_TIMESTAMP(expiry_date) as 'expire_date' FROM...... WHERE....  LIMIT 1");
Do the query, get the array and use:

$expires = $result[expire_date];  //gives us unix timestamp  of expiry date

$rightnow = time();  // gives us unix timestamp for rightnow

if ($expires > $rightnow) {
   echo "EXPIRED!";
} else {
   echo "Current!";
}

R
0
 
LVL 4

Expert Comment

by:rbartz
ID: 16778880
Sorry, got the if else logic backward, my fingers are tired.

EXPIRED and Current are reversed!

Or use if ($expires < $rightnow) {

0
 

Author Comment

by:sabecs
ID: 16778909
Thanks for your response, but I would prefer not to use a mysql query to format the date.
I already have a PHP variable set as a my date.
$expdate = '2006-07-14';
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Accepted Solution

by:
rbartz earned 2000 total points
ID: 16779004
ok, that is easy then!

Use strtotime on my date:

$expdate = '2006-07-14';

$expdate_nix = strtotime($expdate);  
$rightnow = time();

if ($expdate_nix < $rightnow) {
  echo "Expired!";
} else {
 echo "Current!";
}

for even SHORTER use you could do this:

if ( strtotime($expdate) < time() ) {
etc....

0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16779095
it is usually a good idea to do the comparison on the db side
select * from table1 where id=$id and thedate < $expired
if a row returned, then it is not expired, if none, then expired
0
 

Author Comment

by:sabecs
ID: 16781024
Thanks rbartz, thats exactly what I was after.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question