Time Difference

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Time Difference

Post by tecktalkcm0391 »

I have this code:

Code: Select all

$release_time = mktime(date("G")+1, date("i"), date("s"), date("m"), date("j"), date("Y"));
	$current_time = mktime(date("G"), date("i"), date("s"), date("m"), date("j"), date("Y"));
$release_time is 1 hours away
$current_time is the time it is...

I want to know how much time is left until the release time..
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Code: Select all

<?php
$release_time = mktime(date("G")+1, date("i"), date("s"), date("m"), date("j"), date("Y"));
$current_time = mktime(date("G"), date("i"), date("s"), date("m"), date("j"), date("Y"));

$time_left = $release_time - $current_time;
$hours = (int)($time_left / (60*60));
$time_left = $time_left - $hours*60*60;
$mins  = (int)($time_left / (60));
$time_left = $time_left - $mins*60;
$secs   = (int)($time_left / (60*60));
?>
EDIT:
More simple way:

Code: Select all

<?php
$release_time = mktime(date("G")+1, date("i"), date("s"), date("m"), date("j"), date("Y"));
$current_time = mktime(date("G"), date("i"), date("s"), date("m"), date("j"), date("Y"));

$time_left = $release_time - $current_time;
$hours = date("G", $time_left);
$mins = date("i", $time_left);
$secs = date("s", $time_left);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

printf (that's the username) has a post in Code Snippets that details the difference in time between two given timestamps.

$current_time could be set to time().
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

I am currently using:

Code: Select all

<?php 
$release_time = mktime(date("G")+1, date("i"), date("s"), date("m"), date("j"), date("Y")); 
$current_time = mktime(date("G"), date("i"), date("s"), date("m"), date("j"), date("Y")); 

$time_left = $release_time - $current_time; 
$hours = date("G", $time_left); 
$mins = date("i", $time_left); 
$secs = date("s", $time_left); 
print($hours.' hour(s)'.$mins.' min(s)'.$secs.' seconds(s)');
?>
And when I ouput time left. I get 16 hours and the right minutes and seconds... why?
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Use this instead:

Code: Select all

<?php
$release_time = mktime(date("G")+1, date("i"), date("s"), date("m"), date("j"), date("Y"));
$current_time = time();

$time_left = $release_time - $current_time;
$hours = (int)($time_left / (60*60));
$time_left = $time_left - $hours*60*60;
$mins  = (int)($time_left / (60));
$time_left = $time_left - $mins*60;
$secs   = (int)($time_left / (60*60));
?>
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

ok wrote:Use this instead:

Code: Select all

<?php
$release_time = mktime(date("G")+1, date("i"), date("s"), date("m"), date("j"), date("Y"));
$current_time = time();

$time_left = $release_time - $current_time;
$hours = (int)($time_left / (60*60));
$time_left = $time_left - $hours*60*60;
$mins  = (int)($time_left / (60));
$time_left = $time_left - $mins*60;
$secs   = (int)($time_left / (60*60));
?>
That code doesn't work either, it keeps giving me, the right hours and mins, but not the right secs, and sometime it gives me negative mins
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you tried printf's code?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

No, I didn't because I couldn't figure out how to use it, but now that I did try harder :oops: ... I got it to work... thanks!
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

tecktalkcm0391: Post the code you run and it's output (I tested it several times before I posted it and it works!)

BTW, I think that the time zone can effect the result.
Post Reply