You are Unregistered, please register to gain Full access.
    

Coders' Paradise Discuss, [PHP] time... at Evolution : RaGEZONE forum; Hello, i am working with time system for my new game if they make a building they get a time ...


RaGEZONE sponsored advertisment:

 
 
LinkBack Thread Tools
Old 08-18-2009   #1 (permalink)
Freelancer WebDevelopment


Rank: Member +
Join Date: Jan 2005
Location: The Netherlands
Posts: 704
Thanked 0 Times in 0 Posts

[PHP] time...

Hello,
i am working with time system for my new game if they make a building they get a time how long it takes before its done but now am getting confused
how i can make it show like this ;

HOURS : MINUTES : SECONDS

i got this ..

PHP Code:
$now date("Y-n-d H:i:s");
    
$to_time=strtotime($logs['time']); //time when it should be done 2009-08-18 23:15:59
$from_time=strtotime($now); // time now 2009-08-18 21:22:59

$timeleft round(abs($to_time $from_time) / 60);

$timeleft str_replace("."":"$timeleft);

echo 
"<tr><td>".$logs['name']."</td><td>".round($timeleft)." minute('s)</td></tr>"
then it shows 114 minute('s)

so how i can make it like :
HOURS : MINUTES : SECONDS

thanks alot so far,
passie
__________________
'Coding is not just a hobby, its a sort of lifestyle'

Last edited by passie; 08-18-2009 at 08:27 PM.
passie is offline   Reply With Quote

RaGEZONE sponsored advertisment:
Old 08-18-2009   #2 (permalink)
Hm.
 
foxx's Avatar


Rank: Moderator
Join Date: Sep 2006
Posts: 4,455
Thanked 25 Times in 19 Posts

Re: [PHP] time...

PHP Code:
<?php
function timeBetween($start_date,$end_date)
{
    
$diff $end_date-$start_date;
    
$seconds 0;
    
$hours   0;
    
$minutes 0;

    if(
$diff 86400 <= 0){$days $diff 86400;}
    if(
$diff 86400 0)
    {
        
$rest = ($diff 86400);
        
$days = ($diff $rest) / 86400;
        if(
$rest 3600 0)
        {
            
$rest1 = ($rest 3600);
            
$hours = ($rest $rest1) / 3600;
            if(
$rest1 60 0)
            {
                
$rest2 = ($rest1 60);
            
$minutes = ($rest1 $rest2) / 60;
            
$seconds $rest2;
            }
            else{
$minutes $rest1 60;}
        }
        else{
$hours $rest 3600;}
    }

    if(
$days 0){$days $days.':';}
    else{
$days false;}
    if(
$hours 0){$hours $hours.':';}
    else{
$hours false;}
    if(
$minutes 0){$minutes $minutes.':';}
    else{
$minutes false;}
    
$seconds $seconds.'';

    return 
$days.''.$hours.''.$minutes.''.$seconds;
}

$one_date date("U");  
$two_date strtotime($logs['time']); //"2009-08-18 23:15:59"
$result timeBetween($one_date$two_date);

if(
strtotime($result)) {
echo 
date('H:i:s'strtotime($result));
} else {
echo 
"<strong>error:</strong> time already passed.";
}  
?>
this rewritten a little
http://www.linein.org/blog/2008/04/0...-dates-in-php/

Last edited by foxx; 08-18-2009 at 09:28 PM.
foxx is offline   Reply With Quote

Endorsement
Old 08-18-2009   #3 (permalink)
Freelancer WebDevelopment


Rank: Member +
Join Date: Jan 2005
Location: The Netherlands
Posts: 704
Thanked 0 Times in 0 Posts

Re: [PHP] time...

Originally Posted by foxx View Post
PHP Code:
<?php
function timeBetween($start_date,$end_date)
{
    
$diff $end_date-$start_date;
    
$seconds 0;
    
$hours   0;
    
$minutes 0;

    if(
$diff 86400 <= 0){$days $diff 86400;}
    if(
$diff 86400 0)
    {
        
$rest = ($diff 86400);
        
$days = ($diff $rest) / 86400;
        if(
$rest 3600 0)
        {
            
$rest1 = ($rest 3600);
            
$hours = ($rest $rest1) / 3600;
            if(
$rest1 60 0)
            {
                
$rest2 = ($rest1 60);
            
$minutes = ($rest1 $rest2) / 60;
            
$seconds $rest2;
            }
            else{
$minutes $rest1 60;}
        }
        else{
$hours $rest 3600;}
    }

    if(
$days 0){$days $days.':';}
    else{
$days false;}
    if(
$hours 0){$hours $hours.':';}
    else{
$hours false;}
    if(
$minutes 0){$minutes $minutes.':';}
    else{
$minutes false;}
    
$seconds $seconds.'';

    return 
$days.''.$hours.''.$minutes.''.$seconds;
}

$one_date date("U");  
$two_date strtotime($logs['time']); //"2009-08-18 23:15:59"
$result timeBetween($one_date$two_date);

if(
strtotime($result)) {
echo 
date('H:i:s'strtotime($result));
} else {
echo 
"<strong>error:</strong> time already passed.";
}  
?>
this rewritten a little
http://www.linein.org/blog/2008/04/0...-dates-in-php/
how i supose to use it?
perhaps it sound as noob way but i use it like this

$now : 2009-08-18 22:36:00
$logs['time'] 2009-09-18 01:30:25

here's my full script how i use it

PHP Code:
<?php
include "config.php";

$query mysql_query("select * from `logs` where `username`='{$_SESSION['username']}'");



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table>
<tr>
<td>Name</td> <td>Timeleft</td>
</tr>
<?php


while($logs mysql_fetch_assoc($query)){
    
function 
timeBetween($start_date,$end_date)
{
    
$diff $end_date-$start_date;
    
$seconds 0;
    
$hours   0;
    
$minutes 0;

    if(
$diff 86400 <= 0){$days $diff 86400;}
    if(
$diff 86400 0)
    {
        
$rest = ($diff 86400);
        
$days = ($diff $rest) / 86400;
        if(
$rest 3600 0)
        {
            
$rest1 = ($rest 3600);
            
$hours = ($rest $rest1) / 3600;
            if(
$rest1 60 0)
            {
                
$rest2 = ($rest1 60);
            
$minutes = ($rest1 $rest2) / 60;
            
$seconds $rest2;
            }
            else{
$minutes $rest1 60;}
        }
        else{
$hours $rest 3600;}
    }

    if(
$days 0){$days $days.':';}
    else{
$days false;}
    if(
$hours 0){$hours $hours.':';}
    else{
$hours false;}
    if(
$minutes 0){$minutes $minutes.':';}
    else{
$minutes false;}
    
$seconds $seconds.'';

    return 
$days.''.$hours.''.$minutes.''.$seconds;
}

$one_date date("U");  
$two_date strtotime($logs['time']); //"2009-08-18 23:15:59"
$result timeBetween($one_date$two_date).'';

if(
strtotime($result)) {
echo 
date('H:i:s'strtotime($result));
} else {
echo 
"<strong>error:</strong> time already passed.";
}  

$now date("Y-n-d H:i:s");
$timeleft timeBetween($now$logs['time']);

echo 
"<tr><td>".$logs['name']."</td><td>".$timeleft." minute('s)</td></tr>";
}
?>
</table>
</body>
</html>
i get this : error: time already passed.33:29

hopefull you can help me again.
__________________
'Coding is not just a hobby, its a sort of lifestyle'

Last edited by passie; 08-18-2009 at 09:41 PM.
passie is offline   Reply With Quote

Endorsement
<a onclick="_gaq.push(['_trackEvent', 'Outgoing', 'click', '/outgoing/http_www_game_advertising_online_com_']);" rel="nofollow" href="http://www.game-advertising-online.com/" target=_blank>Game Advertising Online</a><br> banner requires iframes

Old 08-18-2009   #4 (permalink)
Hm.
 
foxx's Avatar


Rank: Moderator
Join Date: Sep 2006
Posts: 4,455
Thanked 25 Times in 19 Posts

Re: [PHP] time...

Well try this.
PHP Code:
<?php
include "config.php";

$query mysql_query("select * from `logs` where `username`='{$_SESSION['username']}'");

function 
timeBetween($start_date,$end_date)
{
    
$diff $end_date-$start_date;
    
$seconds 0;
    
$hours   0;
    
$minutes 0;

    if(
$diff 86400 <= 0){$days $diff 86400;}
    if(
$diff 86400 0)
    {
        
$rest = ($diff 86400);
        
$days = ($diff $rest) / 86400;
        if(
$rest 3600 0)
        {
            
$rest1 = ($rest 3600);
            
$hours = ($rest $rest1) / 3600;
            if(
$rest1 60 0)
            {
                
$rest2 = ($rest1 60);
            
$minutes = ($rest1 $rest2) / 60;
            
$seconds $rest2;
            }
            else{
$minutes $rest1 60;}
        }
        else{
$hours $rest 3600;}
    }

    if(
$days 0){$days $days.':';}
    else{
$days false;}
    if(
$hours 0){$hours $hours.':';}
    else{
$hours false;}
    if(
$minutes 0){$minutes $minutes.':';}
    else{
$minutes false;}
    
$seconds $seconds.'';

    return 
$days.''.$hours.''.$minutes.''.$seconds;
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table>
<tr>
<td>Name</td> <td>Timeleft</td>
</tr>
<?php
while($logs mysql_fetch_assoc($query)){
    
    
$one_date date("U");  
    
$two_date strtotime($logs['time']); //"2009-08-18 23:15:59"
    
$result timeBetween($one_date$two_date);

    if(
strtotime($result)) {
        
$message date('H:i:s'strtotime($result));
    } else {
        
$message "time passed.";
    }  
    echo 
"<tr><td>".$logs['name']."</td><td>".$message."</td></tr>";
}
?>
</table>
</body>
</html>
There might be some problems when the difference is longer than 24 hours though. You'd need to rewrite that original script bit more, in that case.

Last edited by foxx; 08-18-2009 at 09:53 PM.
foxx is offline   Reply With Quote
Old 08-18-2009   #5 (permalink)
#$%#@$%!##!#!
 
Hubba's Avatar


Rank: Member +
Join Date: Jan 2009
Location: Ontario Canada
Posts: 697
Thanked 210 Times in 50 Posts

Re: [PHP] time...

Hey thanks i was looking for something like this! +1 mate.
__________________
If your looking at this.. i dont like you :)
Hubba is offline   Reply With Quote
 

Bookmarks

Thread Tools




no new posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290