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
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Sun Dec 03, 2006 4:15 pm
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..
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Sun Dec 03, 2006 4:19 pm
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);
?>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Dec 03, 2006 5:47 pm
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() .
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Wed Dec 06, 2006 2:11 pm
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?
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Wed Dec 06, 2006 2:25 pm
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));
?>
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Wed Dec 06, 2006 5:39 pm
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Dec 06, 2006 5:45 pm
Have you tried printf's code?
tecktalkcm0391
DevNet Resident
Posts: 1030 Joined: Fri May 26, 2006 9:25 am
Location: Florida
Post
by tecktalkcm0391 » Wed Dec 06, 2006 6:56 pm
No, I didn't because I couldn't figure out how to use it, but now that I did try harder
... I got it to work... thanks!
ok
Forum Contributor
Posts: 393 Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land
Post
by ok » Thu Dec 07, 2006 5:51 am
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.