Page 1 of 1

Simple time question.

Posted: Fri Apr 09, 2010 3:53 pm
by Worqy
Hi.
How do I add a time to another time.
Example:

Code: Select all

<?php
date_default_timezone_set('Europe/Helsinki');
$localtime = date("H:i:s");

$buildtime = 00:00:10 // In the real code, I get the time from MySQL

echo $localtime + $buildtime;
If I do it this way the answer is:
23
Values:
Localtime 23:47:57
Buildtime: 00:10:00

I would want the value to be 23:57:57

//Kevin

Re: Simple time question.

Posted: Fri Apr 09, 2010 4:36 pm
by AbraCadaver
$localtime is actually a point in time and by the way you are adding these I am assuming that $buildtime is a measurement of elapsed time not a point in time? So build time 00:10:00 is not 10 minutes after midnight, it is a measurement 0 hours, 10 minutes and 0 seconds, yes? If so, then here's a way:

Code: Select all

$localtime = time();
$buildtime = "00:10:00";
list($h, $m, $s) = explode(":", $buildtime);

echo date("H:i:s", strtotime("+$h hours $m minutes $s seconds", $localtime));
The easiest would be to store the $buildtime in seconds, in this case 600. If that's not possible, then this will work:

Code: Select all

$localtime = time();
$buildtime = preg_replace('/([\d]{2}):([\d]{2}):([\d]{2})/e', "$1*3600+$2*60+$3", "00:10:00");

echo date("H:i:s", $localtime + $buildtime);

Re: Simple time question.

Posted: Fri Apr 09, 2010 5:02 pm
by AbraCadaver
Too much fun:

Code: Select all

$localtime = time();
$buildtime = "00:10:00";

echo date("H:i:s", $localtime+(strtok($buildtime, ":")*3600+strtok(":")*60+strtok(":")));

Re: Simple time question.

Posted: Sat Apr 10, 2010 2:51 am
by Worqy
Yeah, its not 10min after midnight. Its a time just as you sayd: 00hours 10 minutes 00 seconds.

Now I tested both codes and they work fine. But when I tried to edite them something strange happened:

Code: Select all

Session_start();

$buildtime = $_SESSION['buildtime'];
$localtime = $_SESSION['localtime'];

echo "Localtime ";
require 'localtime.php';

echo "<br>";

echo "Buildtime: ";
echo $buildtime;

echo "<br>";

echo "Ready at: ";

list($h, $m, $s) = explode(":", $buildtime);

echo date("H:i:s", strtotime("+$h hours $m minutes $s seconds", $localtime));
Thats the new code, and the output is:
Localtime 10:45:22
Buildtime: 00:10:00
Ready at: 02:10:10

You see here that $localtime is now from a session but when I echo it the time is right ( same as it would be $localtime = time(); ). But still I dont get the right "Ready at: " time ?

EDIT: Now I tried another code, but still get this output:
Localtime 18:51:35
Buildtime: 00:10:00
Ready at: 02:10:18

code:

Code: Select all

<?php



Session_start();
date_default_timezone_set('Europe/Helsinki');


$buildtime = $_SESSION['buildtime'];
$localtime = date("H:i:s");

echo "Localtime ";
echo $localtime;

echo "<br>";

echo "Buildtime: ";
echo $buildtime;

echo "<br>";

echo "Ready at: ";

list($h, $m, $s) = explode(":", $buildtime);
echo date("H:i:s", strtotime("+$h hours $m minutes $s seconds", $localtime));


?>