Jump to content

Add extra seconds to a Date variable


nightkarnation

Recommended Posts

Hey guys!

I have the following doubt,

I have a Date value and I want to add to that date 30 seconds plus.

Example:

 

$last_time = date('D M j H:i:s \G\M\TO Y'); //echo: Thu Mar 10 18:33:48 GMT-0300 2011

 

I need the following date: Thu Mar 10 18:33:48 GMT-0300 2011 to become:

Thu Mar 10 18:34:18 GMT-0300 2011

This means that the retrieved Date now has 30 more seconds...

 

Any ideas??

Looking forward to any help,

 

Thanks a lot in advance,

Cheers!

Link to comment
Share on other sites

Hello Mahngiel

 

First of all, thanks a lot for your kind help.

 

I have googled php time and php date but couldnt find what I was looking for,

 

I am trying to implement the line of code you gave me but I cant seem to get it to work...

 

I tried the awful code below:

 

$addtime = time() + ( 0* 0 * 0 * 30);
echo "last_time=".$last_time + $addtime;
$last_time = $last_time + $addtime;

 

Obviously is not working, any ideas and/or suggestions?

 

Thanks a lot in advance!

Link to comment
Share on other sites

Thanks a lot JcBones!!

 

That works great!! but I have one more problem though and I really think you can fix this easily...

 

$last_time already has the date as a value ($last_time already equals: Thu Mar 10 18:33:48 GMT-0300 2011)

But now I need $last_time to +30 seconds...

 

I tried:

 

$last_time = date('$last_time',strtotime('+30 seconds'));

 

But obviously is not working, any ideas and/or suggestions?

 

Thanks a lot again!

Link to comment
Share on other sites

$time = time();
$last_time = date('D M j H:i:s \G\M\TO Y',$time); //echo: Thu Mar 10 18:33:48 GMT-0300 2011
echo $last_time;

$last_time_plus_30 = date('D M j H:i:s \G\M\TO Y',strtotime('+30 seconds',$time));
echo $last_time_plus_30;

 

Will work even if there are a couple seconds execution time between the date() functions.

Link to comment
Share on other sites

Guys...I am not making myself clear and I apologize for that.

 

$last_time already equals Thu Mar 10 18:33:48 GMT-0300 2011

 

I am grabbing $last_time with that value already from DB...

 

I need now to add 30 seconds to $last_time that has that date...

 

Based on what you guys tought me, I tried this line:

 

$last_time = date($last_time,strtotime('+30 seconds'));

 

It is working but the date format changed a bit and I need the format to be exactly the same only with the 30 seconds added.

 

Thu Mar 10 18:33:48 GMT-0300 2011  (Already grabbed from server)

Thu Mar 10 18:34:18 GMT-0300 2011 (Is my goal with script modification)

 

Any ideas??

Thanks a lot !!

Link to comment
Share on other sites

If you're only expecting one row to be returned from the query, you don't need the while loop. You should also check if you got a result from the query, if you didn't that would explain the results, since $last_time wouldn't be set.

<?php
$result = mysql_query("SELECT Last_User_Time FROM ATS_MAIN WHERE Id = '$id'");
if (mysql_numrows($result) > 0) {
   $row=mysql_fetch_assoc($result);
   $last_time = $row['Last_User_Time'];
   echo $last_time . '<br>';  // just checking
   $new_last_time = date('D M j H:i:s \G\M\TO Y',strtotime($last_time) + 30);
   echo $new_last_time;
} else {
   echo "No data returned from the query";
}
?>

 

Ken

 

Link to comment
Share on other sites

If you just need to add 30 seconds to the time stored in the database you may find it easier and more efficient to do it directly in the query.

 

]SELECT DATE_ADD(Last_User_Time, INTERVAL 30 SECOND) AS Last_User_Time FROM ATS_MA . . . 

Link to comment
Share on other sites

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.