saadlulu
saadlulu

Reputation: 1395

php time and date function

I have a quickie here, Whenever I try to echo out the time in hours:min:sec using the date() function everything works perfect. But when I try echoing it out using a variable with a value, it always adds up 2 hours.

Take a look at the code:

$time = time();

$past = 120;

//this works perfectly

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

//but this doesnt. it adds 2 hours.

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

Upvotes: 0

Views: 221

Answers (2)

Piotr Salaciak
Piotr Salaciak

Reputation: 1663

Watch out what You are doing:

//You are assigning a string to $time variable

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

//second call - trying to format date from unix timestamp, which actually is a string with some hours, minutes and seconds

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

EDIT

Maybe You mean this?

$time = time();
$past = 120;
echo date("H:i:s",$time);
echo date("H:i:s",$time - $past);

Upvotes: 1

miqbal
miqbal

Reputation: 2227

string date ( string $format [, int $timestamp ] )

On second using of "date" function, second param is string. Like that 01:01:01. But it must be integer. So converting 01:01:01 to integer; it will be "0". What's your purpose?

Upvotes: 4

Related Questions