Simple time question.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Worqy
Forum Newbie
Posts: 20
Joined: Tue Feb 02, 2010 6:41 am

Simple time question.

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Simple time question.

Post 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);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Simple time question.

Post 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(":")));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Worqy
Forum Newbie
Posts: 20
Joined: Tue Feb 02, 2010 6:41 am

Re: Simple time question.

Post 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));


?>
Post Reply