Reputation: 63
I have converted the current UTC
time to IST
, and now I need to convert this time to one hour before,and store it in a variable.
<?php
date_default_timezone_set('Asia/Kolkata');
$DateTime=date('Y-m-d H:i:s');
$DateTime=date('H:i:s',$DateTime - 3600);//This does not seem to work
echo $DateTime;
?>
Upvotes: 0
Views: 220
Reputation: 15141
Hope this will work.
<?php
date_default_timezone_set('Asia/Kolkata');
$DateTime=date('Y-m-d H:i:s');
$DateTime=date('H:i:s', strtotime($DateTime) - 3600);
echo $DateTime;
?>
Upvotes: 1
Reputation: 11320
You missed the strtotime.
strtotime will give you the unix timestamp of your date. From that timestamp, you then subtract your one hour
You should do date('H:i:s',strtotime($DateTime)-3600);
So, It should be
<?php
date_default_timezone_set('Asia/Kolkata');
$DateTime=date('Y-m-d H:i:s');
$DateTime=date('H:i:s',strtotime($DateTime) - 3600);
echo $DateTime;
?>
Here's the eval link
Upvotes: 1
Reputation: 2010
For the operation you want to perform you need to use timestamp. You can not add or subtract a number from date string.
Use something like this
$timeBeforeAnHour = date('H:i:s',time() - 3600);
This would help.
Upvotes: 1