Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, need to subtract 12 hours and 30 minutes from a DateTime

Tags:

date

php

datetime

I have a PHP DateTime variable.

How can I reduce or subtract 12hours and 30 minutes from this date in at PHP runtime?

like image 506
gourav Avatar asked Apr 24 '11 05:04

gourav


Video Answer


2 Answers

Subtract 12 Hours and 30 minutes from a DateTime in PHP:

$date = new DateTime();
$tosub = new DateInterval('PT12H30M');
$date->sub($tosub);

The P stands for Period. The T stands for Timespan.

See DateTime, DateTime::sub, and DateInterval in the PHP manual. You'll have to set the DateTime to the appropriate date and time, of course.

like image 81
Rafe Kettler Avatar answered Oct 20 '22 09:10

Rafe Kettler


Try with:

$date = new DateTime('Sat, 30 Apr 2011 05:00:00 -0400');
echo $date->format('Y-m-d H:i:s') . "\n";
$date->sub(new DateInterval('PT12H30M'));
echo $date->format('Y-m-d H:i:s') . "\n";

//Result

2011-04-30 05:00:00
2011-04-29 16:30:00
like image 23
fitorec Avatar answered Oct 20 '22 09:10

fitorec