sgkdnay
sgkdnay

Reputation: 327

PHP Add Seconds

For the life outta me, I checked through-out this forum and follow instruction, might have overlooked a thing or two.

Using:

$today = date("m/d/Y h:i:s A"); 

This will show me the accurate today's date/time, which I'm happy however, I'm trying to add seconds to the current date/time but keep giving me odd results.

$lSec = intval($str);
$lDay = intval($lSec / 86400);
$lHou = intval($lSec / 3600);
WHILE ($lHou >= 24) {
    $lHou = $lHou-24;
};
$lMin = intval(($lSec / 60) % 60);
$lSec = intval($lSec % 60);
return date(
    "m-d-Y H:i:s A",
    mktime(
        date("H")+$lHou,
        date("i")+$lMin,
        date("s")+$lSec,
        date("m")+0,
        date("d")+$lDay,
        date("Y")+0
    )
);

Is there anything I'm missing or is there anything better than this? I even tried date_add and not giving me desirable result either.

Thanks!

P.S. Running on web host IIS7 with PHP 5 support.

Upvotes: 2

Views: 3709

Answers (4)

Add 30 seconds from right now:

$halfMinuteLater = strtotime("+30 seconds");

$formmatedTime = date('H:i:s', $halfMinuteLater);

Upvotes: 1

Bhavika Kathiriya
Bhavika Kathiriya

Reputation: 94

$dateval= "04/07/2016 6:58:00";
$endDate=date('Y-m-d H:i:s',strtotime("+1 seconds",strtotime($dateval)));

Upvotes: 0

strager
strager

Reputation: 90062

You want to add intval($str) seconds to the current time, and format the resulting date?

date("m/d/Y h:i:s A", time() + intval($str));

time gives the current time in seconds, and date accepts a time in seconds.

Upvotes: 1

Kerry Jones
Kerry Jones

Reputation: 21858

Try adding seconds to time()

$today = date( "m/d/Y h:i:s A", time() + 3600 ); // + 1 hour

Upvotes: 3

Related Questions