Jump to content

Need to calculate number of days between two dates... how?


simcoweb

Recommended Posts

I have people posting ads and when the ad is posted I have two fields populated at that time:

 

date_created

date_expired

 

With the date expired being 7 days after the date created. Both are in this format: 03/12/07

 

What I am doing is preventing someone from posting more than one ad per 7 days. I'm validating against their email address for that. But I want to display the date that they would be eligible as well as determine if they are eligible to post or not which would require comparing today's date with probably using the now() function against the date_expired in the database. If the today's date is past then no problem. If not, then it would display an error. Here's what I have on this right now:

 

<?php
// duplicate email check
function emailCheck()
{
  $email = $_POST['email'];
  $sql = "SELECT * FROM users WHERE email='$email'";
  $results = mysql_query($sql) or die(mysql_error());
  $newad_date = $results['date_created'];
  $num_rows = mysql_query($results);
  if ($num_rows >= 0){
    echo "<h3>Duplicate Ad Error</h3><p>You are allowed one ad per every 7 days. You will be eligible to sumit a new ad on $newad_date.";
}
}
?>

of course you could use a MySql built in function called DATEDIFF()

 

$query = "SELECT DATEDIFF(date_created, date_expired) AS difference from table WHERE id = '$id'";
$result = mysql_query($query) or die ("Query was rubbish" . mysql_error());
while($row = mysql_fetch_assoc($result))
{
    if ((difference > 7)||(difference < -7)) // you may have fed the numbers in the wrong way round
    { // post away !!!
    }
    else
    { //no more posts for you old chap
    }
}

Thanks to each and every one of you for the responses!

 

I like the idea of the DATEDIFF function simply because I didn't insert the dates as timestamps and i'd have to convert them to timestamps then back again, etc. if I did it another way. I'll plug in that code and read up on the DATEDIFF function.

Thanks to each and every one of you for the responses!

 

I like the idea of the DATEDIFF function simply because I didn't insert the dates as timestamps and i'd have to convert them to timestamps then back again, etc. if I did it another way. I'll plug in that code and read up on the DATEDIFF function.

 

Another way to allow MySQL to do the work for you is simply to query based on your day limit. Then, you throw it into your system the way you have it, and you should be good to go: if there is no record returned, let them post, otherwise, they're not eligible yet.

SELECT * FROM users
WHERE email = '$email' 
AND DATE(date_created) >= CURDATE() - INTERVAL 7 DAY;

 

This returns records only if there is one that has been posted in the last 7 days matching the email address provided. Then, you handle the results just as you are in the OP.

 

Good luck.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.