Iris Tako
Iris Tako

Reputation: 143

time and date functions gives wrong output in php

What I'm trying to do is to return the rest of this simple equation as date.

$time = (time()+(60*60*12)) - time();
$date = date('d H:i:s', $time);
echo $date;

As you see i have added 12 hours, but this added 1 day + 2 hours. And i stuck here :/.

Output is 01 14:00:00

Expected is 00 12:00:00

So what i'm doing wrong?

EDIT

I have tried date_default_timezone_set() function but it seems to not work for me :/

NEW EDIT

I realize when i do this.

$time = time() - time();
$date = date('d H:i:s', $time);
echo $date;

Print out 01 02:00:00 what is that?

Upvotes: 2

Views: 92

Answers (2)

Wietse
Wietse

Reputation: 370

Not the answer to your question, but take a look at Carbon, "A simple PHP API extension for DateTime.". Very useful for these kind of DateTime-problems.

Upvotes: -1

Sohel0415
Sohel0415

Reputation: 9853

You can do this using DateTime-

$start = new DateTime();
$end = new DateTime();
$end->modify('+12 hour');
$interval = $end->diff($start);
$elapsed = $interval->format('%a days %h hours %i minutes %s seconds');

echo $elapsed;

Upvotes: 2

Related Questions