Jump to content

Recommended Posts

Ok, I have a variable ($incubation) set as 04:00:00.

Then I have another variable ($starttime) set to the current time.

Both are printing out fine.

But I'm trying to get an $endtime from adding the incubation time to the start time.

 

$incubation = $row['incubation']; //IM GRABBING THIS TIME FROM THE DATABASE. It prints 04:00:00

$starttime = date("H:i:s"); //prints 16:23:39

$endtime = date("H:i:s", $starttime+$incubation);  //prints 20:00:00 when it's suppose to print 20:23:39

Link to comment
https://forums.phpfreaks.com/topic/236134-adding-4-hours-to-current-time/
Share on other sites

If you want to do this in PHP, you want to convert the strings to unix timestamps, using strtotime()

 

If you want to do this in SQL, you probably want to use

 

SELECT DATE_FORMAT( ADDTIME(`start`,`incubation`), '%W %M %Y %H:%i:%s') as `finish` FROM `table`

 

Alternately, You can use UNIX_TIMESTAMP(`start`) and TIME_TO_SEC(`time`) as a better way to solve this.

 

Oops. I see you don't store your start time. Well, replace instances of `start` with NOW() in SQL

Requinix, your result gives me : 20:57:26. It's suppose to be 8:57:26.

 

Xyph, your result gives me : 04:00:00

 

EDIT

I even tried it this way:

	$incubation = $row['incubation'];
$incubation = strtotime($incubation);

$starttime = date("H:i:s");
$starttime = strtotime($starttime);

$endtime = $starttime+$incubation;
$endtime = date("H:i:s", $endtime);

echo $endtime;

My result won't be something you can just copy and paste into your code.

 

I'm trying to help you find the best solution, not code for you.

 

If you change your query to return `incubation` in seconds (using TIME_TO_SEC()) then you can simply use

 

echo date('H:i:s', time() + $row['incubation_seconds'])

 

SELECT TIME_TO_SEC(`incubation`) as `incubation_seconds` FROM `table`

$sql = "SELECT TIME_TO_SEC('incubation') as 'incubation_seconds'
FROM eggs WHERE eggid='".$_POST['eggid']."'";
$result = mysqli_query($cxn, $sql) or die(mysqli_error($cxn));
$row = mysqli_fetch_assoc($result);

$endtime = date("H:i:s", time()+$row['incubation_seconds']);
echo $endtime;

 

It's displaying today's current time.

And I'm not going to learn a code that doesn't work. I learn from codes that work because I read over them and then I finally understand what it's doing..

 

EDIT

	$sql = "SELECT incubation
FROM eggs WHERE eggid='".$_POST['eggid']."'";
$result = mysqli_query($cxn, $sql) or die(mysqli_error($cxn));
$row = mysqli_fetch_assoc($result);
$incubation = $row['incubation'];
$incubation = strtotime($incubation);

$starttime = date("H:i:s");

$endtime = date("H:i:s", time()+$incubation);
echo $endtime;

 

And this one works on the minutes and seconds, but it's setting the hours to 0

 

EDIT

WAIT, I take that back. It's jumping 3 more hours onto each incubation time. Why is it doing that?

Ok I'm sorry to bring this back up.

But I just realized I needed a DATETIME instead of TIME.

I've been trying for the last hour to get it to work.. I just can't. I thought it would be easy since I understood the time, but I just can't do it..

 

{
$sql = "SELECT incubation
FROM eggs WHERE eggid='".$_POST['eggid']."'";
$result = mysqli_query($cxn, $sql) or die(mysqli_error($cxn));
$row = mysqli_fetch_assoc($result);
$incubation = $row['incubation'];
echo $incubation."<br>";

$starttime = date("Y-m-d H:i:s")."<br>";
print_r($starttime);

$endtime = date("Y-m-d H:i:s", $starttime+$incubation);
print_r($endtime);
$sql2 = "UPDATE incubator SET status='incubating', starttime='".$starttime."', endtime='".$endtime."' WHERE userid='".$_SESSION['userid']."' AND eggid='".$_POST['eggid']."";
mysqli_query($cxn, $sql2);
$hours = intval(intval($row['incubation_seconds']) / 3600);
$error = "<p align=\"center\">You have started the incubation. You're egg will be fully incubated in ".$hours." hours.</p>";
}

 

The endtime keeps coming out : 1969-12-31 19:33:31

... It's definitely not 1969.. o.o

that because you have the $starttime+$incubation, remember $starttime is now a string, so you need to convert to a time or as its the currect time just use time()

ie

	$starttime = date("Y-m-d H:i:s")."<br>";
print_r($starttime);

$endtime = date("Y-m-d H:i:s", time()+$incubation);
print_r($endtime);

 

EDIT also i am not sure if this topic is solved or not !

if its not then click "Topic Not solved" at the bottom.. or people will skip the thread

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.