Jump to content

Recommended Posts

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:47
Ends on: Jul-14-06 9:55:47
Remaining: 15,7:00:00 - it is giving me the right amount of days,min,secs but my hours are off
Link to comment
https://forums.phpfreaks.com/topic/13206-remaining-time-between-two-dates/
Share on other sites

for start, you should know function date return a time since January 1 1970 00:00:00 GMT
so when you do
print 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.
[!--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:47
Ends on: Jul-14-06 9:55:47
Remaining: 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
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:15
2006-07-14 11:07:15



Also, 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.
[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 here
echo '<br>' . date("Y-m-d H:m:s",$timestamp_future);

echo "<br>" . date("M-d-y g:i:s",$t);
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 here
echo '<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:42
Future Time: 2006-07-14 12:07:42
Formatted Future: Jun-29-06 12:18:42
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.