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
+4
A:
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);
soulmerge
2009-05-09 08:45:34
+2
A:
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
VolkerK
2009-05-09 08:59:15
A:
If its just seconds, you can use strtotime() function that returns a representation of time in seconds (actually, seconds since UNIX epoch). You can do the maths afterwards.
$ts = strtotime('2009-01-14 06:38:18');
$ts -= 750;
echo date( "Y-m-d H:i:s", $ts );
Salman A
2009-05-09 12:53:03