shanawar jamil
shanawar jamil

Reputation:

How to add seconds in a PHP date time

How can I add second(s) in a date time. ex. I have date time 2009-01-14 06:38:18 I need to add -750 seconds in that date and want result in datetime

Upvotes: 1

Views: 2833

Answers (2)

VolkerK
VolkerK

Reputation: 96189

strtotime() also supports some date/time arithmetic.

$ts = strtotime('2009-01-14 06:38:18 -750 seconds');
echo date('Y-m-d H:i:s', $ts);

or e.g.

$ts = strtotime('2009-01-14 06:38:18 next monday');
echo date('Y-m-d H:i:s', $ts);
prints 2009-01-19 00:00:00

Upvotes: 2

soulmerge
soulmerge

Reputation: 75774

You need to convert that date to a unix timestamp, where such operations are trivial:

$unix_timestamp = strtotime('2009-01-14 06:38:18');
$new_string = date('Y-m-d H:i:s', $unix_timestamp - 750);

Upvotes: 6

Related Questions