leachus2002 Posted July 6, 2010 Share Posted July 6, 2010 Hi All, I was wondering if PHP is able to display the number of days until a specific date? For example - 10 days until July 16th? Thanks Matt Quote Link to comment https://forums.phpfreaks.com/topic/206887-counting-number-of-days-until-a-specific-date/ Share on other sites More sharing options...
bh Posted July 6, 2010 Share Posted July 6, 2010 Hi, Your friend will be the time() function and strtotime(). Example: $seconds = time() - strtotime(str_datatime_format); Quote Link to comment https://forums.phpfreaks.com/topic/206887-counting-number-of-days-until-a-specific-date/#findComment-1081898 Share on other sites More sharing options...
leachus2002 Posted July 6, 2010 Author Share Posted July 6, 2010 Thanks for the prompt reply! But how to I specify the date in there? Quote Link to comment https://forums.phpfreaks.com/topic/206887-counting-number-of-days-until-a-specific-date/#findComment-1081899 Share on other sites More sharing options...
ChemicalBliss Posted July 6, 2010 Share Posted July 6, 2010 Easy, you can even do seconds, in fact its easier to do seconds with timestamps: <?php // The time your going to check against $date1 = "25/12/2010"; // Christmas! // Current timestamp (Current Date/Time) $current = time(); // Convert that date string above into a timestamp as well. $until = strtotime($date1); // Get the difference in seconds between two dates. (Very simple math) $difference = $until - $current; // Get number of days (Also very simple math) $days = (($difference / 60) / 60) / 24; // Seconds / minutes / hours / days // Results echo("Number of days until christmas: ".$days." - Number of Seconds: ".$difference."<br />"); ?> Hope this helps, -cb- Quote Link to comment https://forums.phpfreaks.com/topic/206887-counting-number-of-days-until-a-specific-date/#findComment-1081900 Share on other sites More sharing options...
leachus2002 Posted July 6, 2010 Author Share Posted July 6, 2010 Hey CB, Nice coding, thanks for sending that over! I have tried it, but unfortunately I am getting a long decimal number - for example if I set the date as christmas - I get 14796.6248598 Any Ideas? Cheers Matt Quote Link to comment https://forums.phpfreaks.com/topic/206887-counting-number-of-days-until-a-specific-date/#findComment-1081910 Share on other sites More sharing options...
Pikachu2000 Posted July 6, 2010 Share Posted July 6, 2010 Look at the date_diff() function. (link) It might work better for this application. Quote Link to comment https://forums.phpfreaks.com/topic/206887-counting-number-of-days-until-a-specific-date/#findComment-1081914 Share on other sites More sharing options...
ChemicalBliss Posted July 6, 2010 Share Posted July 6, 2010 Oops, strtotime doesnt understand " / ", change the " / " in the date to " - ", eg: 25-12-2010 Also you can use round() to get a less specific long number. or floor() to remove the decimal entirely. -cb- NOTE: As pikachu said you could use the DateTime Object (if you have PHP 5.3 or greater). - To get your version use phpinfo(); Quote Link to comment https://forums.phpfreaks.com/topic/206887-counting-number-of-days-until-a-specific-date/#findComment-1081918 Share on other sites More sharing options...
AbraCadaver Posted July 6, 2010 Share Posted July 6, 2010 Oops, strtotime doesnt understand " / ", change the " / " in the date to " - ", eg: 25-12-2010 Also you can use round() to get a less specific long number. or floor() to remove the decimal entirely. -cb- NOTE: As pikachu said you could use the DateTime Object (if you have PHP 5.3 or greater). - To get your version use phpinfo(); strtotime() treats dates with / as mm/dd/yyyy and dates with - as dd-mm-yyyy or yyyy-mm-dd. Quote Link to comment https://forums.phpfreaks.com/topic/206887-counting-number-of-days-until-a-specific-date/#findComment-1081925 Share on other sites More sharing options...
leachus2002 Posted July 7, 2010 Author Share Posted July 7, 2010 Hey Everyone, I have tried incorporating round() and datediff() into this, but I am struggling to get my head around it, can someone point me in the right direction please? Cheers Matt Quote Link to comment https://forums.phpfreaks.com/topic/206887-counting-number-of-days-until-a-specific-date/#findComment-1082294 Share on other sites More sharing options...
myrddinwylt Posted July 7, 2010 Share Posted July 7, 2010 Hello, Here is a very simple way to get this done // Define final day -- if this is less than the $startdate, then I am not sure what will happen ^.^ $enddate = "2010-12-25"; // Calculation of difference between now, and the end date $startdate = time(); $enddate = strtotime($enddate); $diff = $enddate - $startdate; $days = ceil($diff / 86400); // Output total echo $days; Quote Link to comment https://forums.phpfreaks.com/topic/206887-counting-number-of-days-until-a-specific-date/#findComment-1082313 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.