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 Link to comment https://forums.phpfreaks.com/topic/149090-solved-get-number-of-days/ 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 Link to comment https://forums.phpfreaks.com/topic/149090-solved-get-number-of-days/#findComment-782840 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. Link to comment https://forums.phpfreaks.com/topic/149090-solved-get-number-of-days/#findComment-782843 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)"; ?> Link to comment https://forums.phpfreaks.com/topic/149090-solved-get-number-of-days/#findComment-782845 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 Link to comment https://forums.phpfreaks.com/topic/149090-solved-get-number-of-days/#findComment-782848 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); ?> Link to comment https://forums.phpfreaks.com/topic/149090-solved-get-number-of-days/#findComment-782849 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 Link to comment https://forums.phpfreaks.com/topic/149090-solved-get-number-of-days/#findComment-782851 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.