Daney11 Posted March 12, 2009 Share Posted March 12, 2009 Hey guys, Im not quite sure on how to get the number of days left from a timestamp in the database 2010-02-20 22:58:00 0 Days Left Any help would be great. Thanks Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 12, 2009 Share Posted March 12, 2009 Hi Assuming you have an actual time stamp (number of seconds since unix epoc) then you could get the number of full days left with floor((time() - $timestamp) / 86400) All the best Keith Quote Link to comment Share on other sites More sharing options...
Daney11 Posted March 12, 2009 Author Share Posted March 12, 2009 No, its actually set out in the database as "0000-00-00 00:00:00" and not in seconds. Quote Link to comment Share on other sites More sharing options...
Mikedean Posted March 12, 2009 Share Posted March 12, 2009 This should work. <?php $date1 = strtotime( "now" ); $date2 = strtotime( "2010-02-20 22:58:00" ); $dateDiff = $date2 - $date1; $daysRemaining = floor($dateDiff/(60*60*24)); echo $daysRemaining . " day(s)"; ?> Quote Link to comment Share on other sites More sharing options...
Daney11 Posted March 12, 2009 Author Share Posted March 12, 2009 Thanks again for your help. $date1 = strtotime(2009-03-12 12:27:00); $date2 = strtotime(2009-04-12 22:58:00); $dateDiff = $date1 - $date2; $daysRemaining = floor($dateDiff/(60*60*24)); Works, however im getting "-32 Days Remaining" Im not sure where the "-" is coming from. Edited, seen your edited version. Thanks a lot Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 12, 2009 Share Posted March 12, 2009 date 1 is before date 2, so when you subtract the unix times it will be a negative number. switch them around or get the absolute value of the number (safer bet) <?php $dateDiff = abs($date1 - $date2); ?> Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 12, 2009 Share Posted March 12, 2009 Hi That is partly because of the order of the dates. Just put the most recent of the 2 date first when subtracting one from the other. The other issue is that is usng the full date / times (which might well be what you want). So if you compared 23:59 on one day with 00:01 on the next day it is returning 120 second then dividing that by the number of seconds in a day and using floor which would return 0. In such a situation you might want to still return 1 day, in which case change it to: $date1 = strtotime(2009-04-12 22:58:00); $date2 = strtotime(2009-03-12 12:27:00); $daysRemaining = floor($date1/(60*60*24)) - floor($date2/(60*60*24)); [/code (or rather than dividing by 60*68*24 you could just chop the time part off the date before passing it to strtime). All the best Keith Quote Link to comment 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.