PHP quesitons - subtracting 3 hours from a datetime object

I have a datetime object that's in the following format:

$time = "2009-05-27 13:45:00";

.. and I need to subtract exactly 3 hours from it.

How would I do that?

Thanks,
- Yvan
egoselfaxisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mostartCommented:
0
aldrouin_Commented:
Here is the code you will need. Obviously, you could have had this answer from the manual that was stated in the other post.
$time = "2009-05-27 13:45:00";
 
date_sub($time, new DateInterval("T3H"));
 
echo $time->format("d-m-Y H:i:s").'<br />';

Open in new window

0
egoselfaxisAuthor Commented:
Aldrouin -- I'm getting the following error message when I try to run the code you've given me:

Fatal error: Call to undefined function date_sub() in D:\Inetpub\antiqueplace\test.php on line 5

I'm running PHP Version 5.2.8
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Ray PaseurCommented:
Try this...
$time = "2009-05-27 13:45:00";
 
$earlier = date('c', strtotime($time . ' - 3 hours'));

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
You will probably find that strtotime() and date() play together in ways that are almost magic.  You can test strtotime() on my web site here:
http://www.laprbass.com/RAY_strtotime.php

HTH, ~Ray
0
egoselfaxisAuthor Commented:
$time = "2009-05-27 13:45:00";
 
$earlier = date('c', strtotime($time . ' - 3 hours'));

Ray -- how would I convert the datetime value back to the same format? (ie: 2009-05-27 13:45:00)

When I run your code as is, . the date gets returned in the following format:

2009-05-27T10:45:00-06:00

0
Ray PaseurCommented:
IIRC the date pattern you want is date('Y-m-d H:i:s')
0
egoselfaxisAuthor Commented:
Nevemind -- got it!

date('Y-m-d H:i:s', strtotime($earlier));
0
Ray PaseurCommented:
Yep, that's it!  That is the ISO8601-format DATETIME string.
<?php // RAY_temp_3_hours.php
error_reporting(E_ALL);
 
// FROM THE OP
$time = "2009-05-27 13:45:00";
 
// SUBTRACT THREE HOURS
$earlier = date('Y-m-d H:i:s', strtotime($time . ' - 3 hours'));
 
echo $earlier;

Open in new window

0
shobinsunCommented:
Hi,

$display_date = date("M D Y H:i:s", strtotime($time)-strtotime("3 hours"));

Hope this will help you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.