timmah1 Posted January 18, 2008 Share Posted January 18, 2008 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 https://forums.phpfreaks.com/topic/86595-solved-subtracting-dates/ Share on other sites More sharing options...
yaz Posted January 18, 2008 Share Posted January 18, 2008 $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 https://forums.phpfreaks.com/topic/86595-solved-subtracting-dates/#findComment-442454 Share on other sites More sharing options...
timmah1 Posted January 18, 2008 Author Share Posted January 18, 2008 Close, just displaying today's date Link to comment https://forums.phpfreaks.com/topic/86595-solved-subtracting-dates/#findComment-442460 Share on other sites More sharing options...
timmah1 Posted January 18, 2008 Author Share Posted January 18, 2008 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 https://forums.phpfreaks.com/topic/86595-solved-subtracting-dates/#findComment-442472 Share on other sites More sharing options...
yaz Posted January 18, 2008 Share Posted January 18, 2008 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 https://forums.phpfreaks.com/topic/86595-solved-subtracting-dates/#findComment-442478 Share on other sites More sharing options...
timmah1 Posted January 18, 2008 Author Share Posted January 18, 2008 This does nothing, my time signed up $row['date'] is stored like 0000-00-00 Link to comment https://forums.phpfreaks.com/topic/86595-solved-subtracting-dates/#findComment-442484 Share on other sites More sharing options...
yaz Posted January 18, 2008 Share Posted January 18, 2008 Replace the $exp= explode(" ",$signedupdate); with $exp= explode("-",$signedupdate); Link to comment https://forums.phpfreaks.com/topic/86595-solved-subtracting-dates/#findComment-442486 Share on other sites More sharing options...
timmah1 Posted January 18, 2008 Author Share Posted January 18, 2008 I understand that, but what your trying to show me is the unix timestamp, I'm not using that. Believe me, I appreciate the help, but I don't use the the timestamp Link to comment https://forums.phpfreaks.com/topic/86595-solved-subtracting-dates/#findComment-442489 Share on other sites More sharing options...
yaz Posted January 18, 2008 Share Posted January 18, 2008 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 https://forums.phpfreaks.com/topic/86595-solved-subtracting-dates/#findComment-442501 Share on other sites More sharing options...
timmah1 Posted January 18, 2008 Author Share Posted January 18, 2008 not quite, but thanks Link to comment https://forums.phpfreaks.com/topic/86595-solved-subtracting-dates/#findComment-442511 Share on other sites More sharing options...
timmah1 Posted January 18, 2008 Author Share Posted January 18, 2008 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 https://forums.phpfreaks.com/topic/86595-solved-subtracting-dates/#findComment-442525 Share on other sites More sharing options...
timmah1 Posted January 18, 2008 Author Share Posted January 18, 2008 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 https://forums.phpfreaks.com/topic/86595-solved-subtracting-dates/#findComment-442541 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.