#1 (permalink)  
Old 09-02-2007, 01:21 PM
Registered User
 
Join Date: Sep 2007
Posts: 6
How to calculate new dates

I need calculate the future... mmm

I need add to actual time 300 seconds or 1000 or 3000 seconds and know the future time...

For example:
If the actual time is:

Day 1
Month 9
Year 07
Hour 01
Minuts 54
Seconds 17

add 60 seconds ...

Day 1
Month 9
Year 07
Hour 01
Minuts 55 ----> +60 seconds, no more 54.. its 55 now
Seconds 17

______________________________

I think do that...

PHP Code:
<?PHP
$more_seconds 
60;

$date date("d");
$month date("m");
$year date("y");
$hours date("H");
$minuts =  date("i");
$seconds date("s");

echo 
"day: $date, month: $month, year: $year, Hour: $hours:$minuts:$seconds";

$seconds $seconds $more_seconds;

while(
$seconds 60){
$seconds $seconds 60;
$minuts $minuts 1;
}

while(
$minuts 60){
$minuts $minuts 60;
$hours $hours 1;
}

while(
$hours 24){
$hours $hours 24;
$date $date 1;
}

$ok false;
while(
$ok == false){

if (
$month == OR $month == OR $month == OR $month == OR $month == OR $month == 10 OR $mont == 12){
if (
$date 31){
$date $date 31;
$month $month 1;
}
else
{
$ok true;
}

}
if (
$month == OR $month == OR $month == OR $month == 11){
if (
$date 30){
$date $date 30;
$month $month 1;
}
else
{
$ok true;
}

}

if (
$month == 02){
if (
$date 28){
$date $date 28;
$month $month 1;
}
else
{
$ok true;
}

}

if (
$month 12){
$month $month 12;
$year $year 1;
}

//end $ok == false

echo "day: $date, month: $month, year: $year, Hour: $hours:$minuts:$seconds";

?>
But that script i make... i think have a error... "leap year years" in spanish "aņos biciestos"... anything can help me?

Last edited by Nuker; 09-02-2007 at 01:44 PM.
Reply With Quote

  #2 (permalink)  
Old 09-02-2007, 01:44 PM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
The easiest way in PHP is to convert your date into a timestamp (look up the strtotime function), then add the number of seconds to the timestamp. Then, you can convert your timestamp back into a date string.

Timestamps are calculated by the number of seconds from midnight on January 1, 1970. Therefore, a timestamp of "60" would be 00:01:00 on Jan. 1, 1970. If you add your current timestamp (which you can get using the Now() function in PHP) plus the number of seconds you want to add (300, 1000, or 3000), you'll get the correct time.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
  #3 (permalink)  
Old 09-02-2007, 03:59 PM
Registered User
 
Join Date: Sep 2007
Posts: 6
tyyyyyyyyyyyy ^^ work perfect !
Reply With Quote
  #4 (permalink)  
Old 09-07-2007, 04:01 PM
Registered User
 
Join Date: Sep 2007
Posts: 6
Hello, i have another problem

In a game, i send a warrior to attack other player, this warrior travell to the enemy city and he have in the data base the time in "hours" "seconds" minuts date month and years to travell

I put a button to abort the travell, in that link i calculate.. the time in the data base of the warrior to finish the travell, minus the current time, the result i add to the current time, and know i have the new time to he must wait to travell from the current position to his "city"


The problem its... the result of all operations.. equal to the first time to he travell to the enemys point :S...

if anything cant understand me pls say.. i post better, sorry my bad english

i post the code, if any can show the error ... :/

PHP Code:
$sql=mysql_query("SELECT userid, victoria, dia, mes, anio, horas, minutos, segundos FROM kw_users WHERE userid = $userid",$con);
while(
$row mysql_fetch_array($sql))
  {
    if (
$row["victoria"] == "llendo"){
    
    
$date $row["dia"];
    
$month $row["mes"];
    
$year $row["anio"];
    
$hours $row["horas"];
    
$minuts $row["minutos"];
    
$seconds $row["segundos"];

$futuro mktime($hours$minuts$seconds$month$date$year) - time();

$futuro time() + $futuro;

$date date("d",$futuro);
$month date("m",$futuro);
$year date("y",$futuro);
$hours date("H",$futuro);
$minuts =  date("i",$futuro);
$seconds date("s",$futuro);

mysql_query("UPDATE kw_users SET dia = $date WHERE userid = $userid");
mysql_query("UPDATE kw_users SET mes = $month WHERE userid = $userid");
mysql_query("UPDATE kw_users SET anio = $year WHERE userid = $userid");
mysql_query("UPDATE kw_users SET horas = $hours WHERE userid = $userid");
mysql_query("UPDATE kw_users SET minutos = $minuts WHERE userid = $userid");
mysql_query("UPDATE kw_users SET segundos = $seconds WHERE userid = $userid");
  } 

Last edited by Nuker; 09-07-2007 at 04:03 PM.
Reply With Quote
  #5 (permalink)  
Old 09-07-2007, 04:30 PM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
To help yourself debug the issue, try echoing some of the values that you're coming up with before actually committing anything to the database. For instance, I would recommend echoing each of your variables as you're working. Something like:
Code:
$sql=mysql_query("SELECT userid, victoria, dia, mes, anio, horas, minutos, segundos FROM kw_users WHERE userid = $userid",$con);
while($row = mysql_fetch_array($sql))
  {
    if ($row["victoria"] == "llendo"){
    
    $date = $row["dia"];
    $month = $row["mes"];
    $year = $row["anio"];
    $hours = $row["horas"];
    $minuts = $row["minutos"];
    $seconds = $row["segundos"];

echo "<div>$date<br />$month<br />$year<br />$hours<br />$minuts<br />$seconds</div>";

$futuro = mktime($hours, $minuts, $seconds, $month, $date, $year) - time();

echo "<div>$futuro</div>";

$futuro = time() + $futuro;

echo "<div>$futuro</div>"

$date = date("d",$futuro);
$month = date("m",$futuro);
$year = date("y",$futuro);
$hours = date("H",$futuro);
$minuts =  date("i",$futuro);
$seconds = date("s",$futuro);

echo "<div>$date<br />$month<br />$year<br />$hours<br />$minuts<br />$seconds</div>";
}
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
  #6 (permalink)  
Old 09-07-2007, 04:34 PM
Registered User
 
Join Date: Sep 2007
Posts: 6
yesssssss
tyyyyyyyyyy

now i know the problem and fix it xD

the problem is in the pluss and minuss xDDDD

now work perfect! ^^

PHP Code:
    $date $row["dia"];
    
$month $row["mes"];
    
$year $row["anio"];
    
$hours $row["horas"];
    
$minuts $row["minutos"];
    
$seconds $row["segundos"];
    
$dateb $row["diab"];
    
$monthb $row["mesb"];
    
$yearb $row["aniob"];
    
$hoursb $row["horasb"];
    
$minutsb $row["minutosb"];
    
$secondsb $row["segundosb"];
    
$A_C mktime($hours$minuts$seconds$month$date$year) - time();
$B_A mktime($hoursb$minutsb$secondsb$monthb$dateb$yearb) - mktime($hours$minuts$seconds$month$date$year);
$futuro $B_A $A_C;
$futuro time() + $futuro;
$date date("d",$futuro);
$month date("m",$futuro);
$year date("y",$futuro);
$hours date("H",$futuro);
$minuts =  date("i",$futuro);
$seconds date("s",$futuro); 
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 07:41 AM.

 
Bitrix
Clicky Web Analytics
CloudContacts
Maxtango


Subscribe to our feed | add to myYahoo!

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
© 1997-2007 HTMLCenter