Jump to content

[SOLVED] Subtracting Dates


timmah1

Recommended Posts

Hello,

 

I'm setting up a members level with dates.

You can have a free sign up and do so much for 30 days, after the 30 days, everything you did will be erased and you can start fresh.

 

Example:

You sign up for the site and your allowed 10 posts for the month 2008-01-17

After the month (30 days), you will be allowed to post 10 more. 2008-02-16 would be 30 days

 

I cannot figure out to subtract days from their sign up date to show how many more days they have left then to let the script allow them to proceed if the month is up.

 

The sign up date is set up like 2008-01-12.

 

Any ideas?

 

I've tried something like this, but it just shows -1

$total = $row['date'];

$today = date("d");
$total = date("d", strtotime("+30 Days"));

$proceed = $total-$today;
echo "$proceed";

 

Thanks in advance

Link to comment
Share on other sites

$exp = explode("-", $row['Date']); //ie 2007-12-25

$joinedmonth = $exp[1];

$joinedday = $exp[2];

$joinedyear = $exp[0];

 

$plusonemonth = date("Y-m-d", mktime(0, 0, 0, $joinedmonth+1, $joinedday,  $joinedyear));

 

echo($plusonemonth);

// 2008-12-25

 

That might help?

Link to comment
Share on other sites

ok, I have this

 

$plusonemonth = date("M j, Y", strtotime("+30 Days"));
$month = date("M j, Y", strtotime("$row[date]"));

 

How do I subtract these two to  know how many days left?

 

I've tried this:

$total = $pluonemonth-$month;

But it just shows 0

Link to comment
Share on other sites

Ah sorry. Sort of missed the point completely...

 

How about this:

 

function get_daysleft($signedupdate) {

$exp= explode(" ",$signedupdate);

$thetime= explode(":", $exp[1]);

$thedate= explode("-", $exp[0]);

 

if(!$thedate[0] && !$thedate[1]) {

return false;

}

 

$now = getdate();

$unixdue = @mktime($thetime[0], $thetime[1], $thetime[2], $thedate[1], $thedate[2], $thedate[0]);

$unixnow = mktime($now['hours'], $now['minutes'], $now['seconds'], $now['mon'], $now['mday'], $now['year']);

$unixtotal = $unixdue - $unixnow;

$total = ((($unixtotal/60) /60)/24);

$total = floor($total);

return $total;

}

 

Link to comment
Share on other sites

Okay, sorry last try. You need the days in between date A and date B correct?

 

This will return it:

 

function get_daysleft($signedupdate) {

      $thedate= explode("-", $signedupdate);

      $now = getdate();

      $unixdue = @mktime(0, 0, 0, $thedate[1], $thedate[2], $thedate[0]);

      $unixnow = mktime($now['hours'], $now['minutes'], $now['seconds'], $now['mon'], $now['mday'], $now['year']);

      $unixtotal = $unixdue - $unixnow;

      $total = ((($unixtotal/60) /60)/24);

      $total = floor($total);

      return $total;

}

 

if you want to find out if 30 days exactly have gone by, then replace "$now = getdate()" with an array of the month/day/year values. The time can be 0.

 

Its just comparing them using unix dates, in case the dates are from different years and stuff.

 

Link to comment
Share on other sites

I keep trying different things, and I keep getting errors

 

This is the code I'm using

$var1 = date("Y-M-j", strtotime("now"));
$var2 = date("Y-M-j", strtotime("$row[date]"));

    //date format: yyyy-mm-dd
    $date_diff = subtract_dates("$var2", "$var1");
    echo $date_diff . " days";

    //function to find difference between two dates
    function subtract_dates($begin_date, $end_date)
    {
    return round(((strtotime($end_date) - strtotime($begin_date)) / 86400));
    }

 

But I get the error

Fatal error: Call to undefined function: subtract_dates() in /home/.... on line 13

Line 13 is this

$date_diff = subtract_dates("$var2", "$var1");

 

Can somebody figure out what I'm doing wrong?

 

Thanks

Link to comment
Share on other sites

For anybody that would be curious as to how to do this, this script works PERFECT for me now

 

$date2=date("m/d/y", strtotime("+31 Days"));
$date1=date("m/d/y", strtotime("$row[date]"));
function date_diff($dformat, $endDate, $beginDate)
{
           $date1=explode($dformat, $beginDate);
           $date2=explode($dformat, $endDate);
           $start_date=gregoriantojd($date1[0], $date1[1], $date1[2]);
           $end_date=gregoriantojd($date2[0], $date2[1], $date2[2]);
           return $end_date - $start_date;
}

print "If we minus " . $date1 . " from " . $date2 . " we get " . date_diff("/", $date2, $date1) . ".";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.