barkster Posted June 29, 2006 Share Posted June 29, 2006 I'm trying to calculate the time remaing in days, hour, min, secs between two timestamps but for some reason I'm not getting correct amount of hours? Here is what I have so far.[code]<? $timestamp_future = time() + (60*60*24*15);$d = $timestamp_future - time();print("Current Time: ");echo(date("M-d-y g:i:s"));echo("<br>Ends on: ");print date("M-d-y g:i:s",$timestamp_future);echo("<br>Remaining: ");print date("j,g:i:s",$d);?>[/code]Returns:Current Time: Jun-29-06 9:55:47Ends on: Jul-14-06 9:55:47Remaining: 15,7:00:00 - it is giving me the right amount of days,min,secs but my hours are off Quote Link to comment https://forums.phpfreaks.com/topic/13206-remaining-time-between-two-dates/ Share on other sites More sharing options...
hvle Posted June 29, 2006 Share Posted June 29, 2006 for start, you should know function date return a time since January 1 1970 00:00:00 GMTso when you doprint date("j,g:i:s",$d);it return time and date since jan 1 1970 00:00:00 plus $d number of second, so that is exactly what you get.$d = $timestamp_future - time();then $d is # of seconds.you need to convert this into hours, mins, sec like this$days = (int) $d / 86400; // 86400 is # of seconds in a day (24 * 60 * 60)$hours = (int) ($d % 86400) / 3600;$mins = (int) ($d % 3600) / 60;$seconds = $d % 60;echo "$days days and $hours:$mins:$seconds";you might want to do some number formatting to make it look nice. Quote Link to comment https://forums.phpfreaks.com/topic/13206-remaining-time-between-two-dates/#findComment-50829 Share on other sites More sharing options...
obsidian Posted June 29, 2006 Share Posted June 29, 2006 [!--quoteo(post=389258:date=Jun 29 2006, 10:02 AM:name=barkster)--][div class=\'quotetop\']QUOTE(barkster @ Jun 29 2006, 10:02 AM) [snapback]389258[/snapback][/div][div class=\'quotemain\'][!--quotec--]I'm trying to calculate the time remaing in days, hour, min, secs between two timestamps but for some reason I'm not getting correct amount of hours? Here is what I have so far.Returns:Current Time: Jun-29-06 9:55:47Ends on: Jul-14-06 9:55:47Remaining: 15,7:00:00 - it is giving me the right amount of days,min,secs but my hours are off[/quote]the date() function takes a valid UNIX timestamp. when you are working with differences, you usually will need to calculate them manually. for instance:[code]function getDiff($time1, $time2) { $diff = abs(strtotime($time1) - strtotime($time2)); $myArr = array(); $min = 60; $hour = $min * 60; $day = $hour * 24; $myArr['days'] = floor($diff / $day); $diff = $diff % $day; $myArr['hours'] = floor($diff / $hour); $diff = $diff % $hour; $myArr['mins'] = floor($diff / $min); $myArr['secs'] = $diff; % $min; return $myArr;}$start = date('Y-m-d h:i:s');$end = "2006-12-25";$diff = getDiff($start, $end);echo "Time until Christmas: $diff[days] Days, $diff[hours] Hours, $diff[mins] Minutes and $diff[secs] Seconds!";[/code]hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/13206-remaining-time-between-two-dates/#findComment-50830 Share on other sites More sharing options...
barkster Posted June 29, 2006 Author Share Posted June 29, 2006 Thanks for the quick reply, I new there was something a bit odd going on and it was me :-) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/13206-remaining-time-between-two-dates/#findComment-50831 Share on other sites More sharing options...
barkster Posted June 29, 2006 Author Share Posted June 29, 2006 Got that working but had a question now, why is my minute always off by one here? I'm trying to add 15 days to a date:[code]$t = time();$timestamp_future = $t + (60*60*24*15);$t = date("Y-m-d H:m:s",$t);$timestamp_future = date("Y-m-d H:m:s",$timestamp_future);echo('<br>'.$t.'<br>'.$timestamp_future);[/code]Returns:2006-06-29 11:06:152006-07-14 11:07:15Also, how can I go from format "Y-m-d H:m:s" to "M-d-y g:i:s" I thought I could do date("M-d-y g:i:s",$t) but of course it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/13206-remaining-time-between-two-dates/#findComment-50859 Share on other sites More sharing options...
hvle Posted June 29, 2006 Share Posted June 29, 2006 [code]$t = time();$timestamp_future = $t + (60*60*24*15);$t = date("Y-m-d H:m:s",$t); //<<<<you changed $t here$timestamp_future = date("Y-m-d H:m:s",$timestamp_future);echo('<br>'.$t.'<br>'.$timestamp_future);[/code]It doesn't work because you changed $t on indicated line.try this:$t = time();$timestamp_future = $t + (60*60*24*15);echo '<br>' . date("Y-m-d H:m:s",$t); //<<<<you changed $t hereecho '<br>' . date("Y-m-d H:m:s",$timestamp_future);echo "<br>" . date("M-d-y g:i:s",$t); Quote Link to comment https://forums.phpfreaks.com/topic/13206-remaining-time-between-two-dates/#findComment-50876 Share on other sites More sharing options...
barkster Posted June 29, 2006 Author Share Posted June 29, 2006 I knew I did something stupid again, I kept looking at it over and over knowing something was off when I looked at it but kept missing. When I run this, they future time is still off by one minute??$t = time();$timestamp_future = $t + (60*60*24*15);echo '<br>Current Time: ' . date("Y-m-d H:m:s",$t); //<<<<you changed $t hereecho '<br>Future Time: ' . date("Y-m-d H:m:s",$timestamp_future);echo "<br>Formatted Future: " . date("M-d-y g:i:s",$t);Returns: Current Time: 2006-06-29 12:06:42Future Time: 2006-07-14 12:07:42Formatted Future: Jun-29-06 12:18:42 Quote Link to comment https://forums.phpfreaks.com/topic/13206-remaining-time-between-two-dates/#findComment-50886 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.