Mavrik347 Posted October 9, 2010 Share Posted October 9, 2010 Hi guys, I've hit a brick wall here and am in need of your help. I'm pretty new to PHP and have limited knowledge to say the least. I'll explain what it is I'm trying to do. Set start date as 01/01/2004 (dmY) $oFour Set how many days has it been since then? $today Set how many days it was from $ofour 30 days ago. $today -30 = $thirtyDaysAgo But the problem is I don't know how to make date('z'); work from 2004 and not 01/01/2010. So $today will be how many days it has been since the start of 2004 and $thirtyDaysAgo will be $today -30. I can set up $thirtyDaysAgo no problem but it's just finding out how to get the $today number... Hope anyone can offer a little light to my situation :/ Mav Quote Link to comment https://forums.phpfreaks.com/topic/215505-how-to-count-days-from-a-certain-date/ Share on other sites More sharing options...
jcbones Posted October 9, 2010 Share Posted October 9, 2010 Check out the DateTime class <?php $date = '2004/10/21'; //get the start date from a form $_POST['date'] $oFour = new DateTime($date); $today = new DateTime(); $date_of_oFour = $oFour->format('m-d-Y'); //accepts any string that date accepts. $interval = $oFour->diff($today); $thirty_days_from_oFour = $oFour->sub(new DateInterval('P30D')); $days_since_oFour = $interval->format('%a days'); $days_subtracted_from_oFour = $oFour->format('m-d-Y'); echo 'Start date: ' . $date_of_oFour . '<br />Days since the start date: ' . $days_since_oFour . '<br />30 days before the start date: ' . $days_subtracted_from_oFour; ?> Tested. Quote Link to comment https://forums.phpfreaks.com/topic/215505-how-to-count-days-from-a-certain-date/#findComment-1120602 Share on other sites More sharing options...
Mavrik347 Posted October 9, 2010 Author Share Posted October 9, 2010 Thanks for the awesome quick reply. But I get: Fatal error: Call to undefined method DateTime::diff() in /home/sever/public_html/mav/datetest.php on line 36 http://www.sev3rance.com/mav/datetest.php Edit: Sorry line 36 is: $interval = $oFour->diff($today); Quote Link to comment https://forums.phpfreaks.com/topic/215505-how-to-count-days-from-a-certain-date/#findComment-1120620 Share on other sites More sharing options...
BlueSkyIS Posted October 9, 2010 Share Posted October 9, 2010 what version of PHP are you using? I'm not sure DateTime is in PHP < 5.x Quote Link to comment https://forums.phpfreaks.com/topic/215505-how-to-count-days-from-a-certain-date/#findComment-1120632 Share on other sites More sharing options...
Mavrik347 Posted October 9, 2010 Author Share Posted October 9, 2010 PHP 5.2.9 The following code from the manual works fine: try { $date = new DateTime('2000-01-01'); } catch (Exception $e) { echo $e->getMessage(); exit(1); } echo $date->format('Y-m-d'); echos 2000-01-01 Quote Link to comment https://forums.phpfreaks.com/topic/215505-how-to-count-days-from-a-certain-date/#findComment-1120673 Share on other sites More sharing options...
Mavrik347 Posted October 9, 2010 Author Share Posted October 9, 2010 DateTime::diff (PHP 5 >= 5.3.0) We are out of date, thanks for the help guys, I'll come back when we are up to date. Quote Link to comment https://forums.phpfreaks.com/topic/215505-how-to-count-days-from-a-certain-date/#findComment-1120675 Share on other sites More sharing options...
gizmola Posted October 10, 2010 Share Posted October 10, 2010 You can still take advantage of unix timestamps to handle these types of problems. // Current Unix timestampdefine('SECONDSPERDAY', 60*60*24);$nowTS = time();// Your old date$oldTS = mktime(0,0,0,1,1,2004);// Date of your old date - 30$oldTS30prior = date('m-d-Y', $oldTS - (30 * SECONDSPERDAY));$daysSince = ($nowTS - $oldTS) / SECONDSPERDAY;echo 'Today:' . date('m-d-Y', $nowTS);echo ' 2004 date: ' . date('m-d-Y', $oldTS);echo " -30 days: $oldTS30prior ";echo " Days from 1-1-2004 to now: $daysSince"; Quote Link to comment https://forums.phpfreaks.com/topic/215505-how-to-count-days-from-a-certain-date/#findComment-1120697 Share on other sites More sharing options...
Mavrik347 Posted October 10, 2010 Author Share Posted October 10, 2010 My host is hopeless and refuses to do his job and update PHP. gizmola that works great. Could you explain the variables and the define('SECONDSPERDAY', 60*60*24); Part? I think your doing it in how many seconds since 2004-01-01, what's the reasoning behind that? Is it possible to do it with days? Quote Link to comment https://forums.phpfreaks.com/topic/215505-how-to-count-days-from-a-certain-date/#findComment-1120707 Share on other sites More sharing options...
Mavrik347 Posted October 10, 2010 Author Share Posted October 10, 2010 With your help, I have done it define('SECONDSPERDAY', 60*60*24); // 86,400 seconds in a day$startDateTime = "2007-04-16 19:47:00";$startYear = substr($startDateTime, 0, 4); // Get join Year (2007)$startMonth = substr($startDateTime, 5, 2); // Get join Month (04)$startDay = substr($startDateTime, 8, 2); // Get join Day (16) $oFour = mktime(0,0,0,1,1,2004); // Get 2004 time$now = time(); // Get the current time$exactDaysSinceFour = ($now - $oFour) / SECONDSPERDAY; // Convert 2004 time into exact days$daysSinceFour = round($exactDaysSinceFour); // Round exact days$thirtyDaysAgo = $daysSinceFour - 30; // Take 30 away from todays days since 2004 to find out what it was 30 days ago$joinTime = mktime(0,0,0,$startMonth,$startDay,$startYear); // Get time character joined corporation$exactJoinDay = ($joinTime - $oFour) / SECONDSPERDAY; // Convert join time into exact days$joinDay = round($exactJoinDay); // Round exact daysif ($joinDay < $thirtyDaysAgo) {echo "Access Granted";} else {echo "Access Denied";} Quote Link to comment https://forums.phpfreaks.com/topic/215505-how-to-count-days-from-a-certain-date/#findComment-1120727 Share on other sites More sharing options...
gizmola Posted October 10, 2010 Share Posted October 10, 2010 Glad we helped. If you get the chance you can mark the topic "Solved" by clicking on the "Mark Solved" button at the bottom of the thread. Quote Link to comment https://forums.phpfreaks.com/topic/215505-how-to-count-days-from-a-certain-date/#findComment-1120859 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.