lanu777 Posted November 15, 2013 Share Posted November 15, 2013 hi there trying to create a very simple date counter script here. I just cant recall how to do all this. There is a span of days starting at oct 30th, 2013 and I want to get todays date and then determine how many days have passed. Am I even on the right track here? Any help would be mmuch appreciated! my feeble attempt is below: <?php $elvenportal = mktime(0,0,0,10,30,2011); $now = mktime(date("H"),date("i"),date("s"),date("n"),date("j"),date("Y")); $theeday = date("d")-1; $late = date("H"); if ($late < 7) {$now = mktime(0,0,0,date("n"),$theeday,date("Y"));}; if ($_POST["dayz"] != "" ){ if ($_POST["month"] != "" ){ if ($_POST["year"] != "" ){ $now = mktime(0,0,0,$_POST["month"],$_POST["dayz"],$_POST["year"]); };};}; $offset = $now - $elvenportal; print $offset; ?> Quote Link to comment https://forums.phpfreaks.com/topic/283909-creating-a-simple-date-counter/ Share on other sites More sharing options...
Barand Posted November 15, 2013 Share Posted November 15, 2013 try this method $adate = new DateTime('2013-10-20'); $bdate = new DateTime('2013-11-01'); $now = new DateTime(); $period = new DatePeriod($adate, new DateInterval('P1D'), $bdate); printf("%-15s\t%s\n", 'Date', 'Days elapsed'); foreach ($period as $dt) { printf("%-15s\t%6d\n", $dt->format('d M Y'), $dt->diff($now)->days); } /* RESULTS *************************************** Date Days elapsed 20 Oct 2013 26 21 Oct 2013 25 22 Oct 2013 24 23 Oct 2013 23 24 Oct 2013 22 25 Oct 2013 21 26 Oct 2013 20 27 Oct 2013 19 28 Oct 2013 18 29 Oct 2013 17 30 Oct 2013 16 31 Oct 2013 15 *****************************************************/ Quote Link to comment https://forums.phpfreaks.com/topic/283909-creating-a-simple-date-counter/#findComment-1458332 Share on other sites More sharing options...
lanu777 Posted November 16, 2013 Author Share Posted November 16, 2013 I tried this and got the following error: Fatal error: Class 'DatePeriod' not found I guess maybe this is from a newer version of php i'm working with? What is the purpose of $bdate? I'm unclear on that. Thanks so much for your help its much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/283909-creating-a-simple-date-counter/#findComment-1458511 Share on other sites More sharing options...
Barand Posted November 16, 2013 Share Posted November 16, 2013 DatePeriod and DateInterval require PHP >= 5.3 $adate and $bdate are the start and end dates for the DatePeriod loop. Quote Link to comment https://forums.phpfreaks.com/topic/283909-creating-a-simple-date-counter/#findComment-1458531 Share on other sites More sharing options...
Barand Posted November 16, 2013 Share Posted November 16, 2013 This should work with just DateTime objects $adate = new DateTime('2013-10-20'); $bdate = new DateTime('2013-11-01'); $now = new DateTime(); #$period = new DatePeriod($adate, new DateInterval('P1D'), $bdate); echo '<pre>'; printf("%-15s\t%s\n", 'Date', 'Days elapsed'); while ($adate < $bdate) { printf("%-15s\t%6d\n", $adate->format('d M Y'), floor(($now->getTimestamp()-$adate->getTimestamp())/86400)); $adate->modify('+1 days'); } echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/283909-creating-a-simple-date-counter/#findComment-1458536 Share on other sites More sharing options...
lanu777 Posted November 16, 2013 Author Share Posted November 16, 2013 hmm my php info says im runnning Version 5.2.17, yet Im getting this error:Fatal error: Call to undefined method DateTime::getTimestamp() Quote Link to comment https://forums.phpfreaks.com/topic/283909-creating-a-simple-date-counter/#findComment-1458617 Share on other sites More sharing options...
Barand Posted November 16, 2013 Share Posted November 16, 2013 I wonder why they introduced DateTime in 5.2 when you can't do anything with it until 5.3 Back to the old-fashioned ways $adate = strtotime('2013-10-20'); $bdate = strtotime('2013-11-01'); $now = mktime(0,0,0); echo '<pre>'; printf("%-15s\t%s\n", 'Date', 'Days elapsed'); while ($adate < $bdate) { printf("%-15s\t%6d\n", date('d M Y', $adate), floor(($now-$adate)/86400) ); $adate = strtotime('+1 days', $adate); } echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/283909-creating-a-simple-date-counter/#findComment-1458621 Share on other sites More sharing options...
lanu777 Posted November 16, 2013 Author Share Posted November 16, 2013 nice thats working, i used it like so: $adate = strtotime('2013-10-30'); $bdate = strtotime('2013-10-31'); $now = mktime(0,0,0); echo '<pre>'; printf("%-15s\t%s\n", ' ', ' '); while ($adate < $bdate) { printf("%-15s\t%6d\n", 'this be how many days passed:<span>', floor(($now-$adate)/86400), '<span>' ); $adate = strtotime('+1 days', $adate); } echo '</pre>';now not exactly sure how to get this number to print to a variable - i tried this and didnot succeed:$howmanydayspassed = printf("%-15s\t%6d\n", floor(($now-$adate)/86400) );Really appreciate your help there you are awesome! Quote Link to comment https://forums.phpfreaks.com/topic/283909-creating-a-simple-date-counter/#findComment-1458623 Share on other sites More sharing options...
Barand Posted November 17, 2013 Share Posted November 17, 2013 $howmanydayspassed = floor(($now-$adate)/86400) ; Quote Link to comment https://forums.phpfreaks.com/topic/283909-creating-a-simple-date-counter/#findComment-1458630 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.