Jump to content

Need to calculate number of days between dates then return a date


simcoweb

Recommended Posts

Summary:

 

* ad posters can create one ad per every 7 days

* their email address is the user id i'm working off of to determine if they've already submitted an ad

* say they try to submit an ad 3 days after submitting a previous ad

* I want a message to say 'You can not post an ad until Saturday... blah blah'.

 

I need to find the date they submitted their current ad which is no problem since it's inserted into the database when they created the ad. Now, it's 3 days later and they want to submit another. I need to determine if it's been 7 days or not and, if not, display an error and, if so, let it go through. Here's what I have but it doesn't work (or why else would I be posting... eh? lol)

 

<?php
$date1 = '11/25/06';
$date2 = "now";
$start = strtotime($date1);
$end = strtotime($date2);
$days = ($date2 - $date1) / (60*60*24);
$diff = number_format($days);
echo $diff;
?>

 

This is not 100% accurate to how it would actually be written but it's my working model to get the math right. The logic here line by line is:

 

1. get the date from the current db entry

2. get today's date

3. calculate the time stamp for the date of their ad posting

4. calculate the time stamp for today

5. calculate the difference

6. print the difference

 

Obviously something is not right as it returns -0 as the value. Help anyone?

This is actually something that is more reliable to perform in a mysql query as mysql's support for date calculations is better than php's....

have a read of this page http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html domewhere in there will be an example doing exactly what you need...

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.