Aastha Kathuria
Aastha Kathuria

Reputation: 63

Create a php variable storing the time one hour before the present IST time

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

Answers (3)

Sahil Gulati
Sahil Gulati

Reputation: 15141

Hope this will work.

Try this code snippet here

<?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

Sulthan Allaudeen
Sulthan Allaudeen

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

Referred from here

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

Parantap Parashar
Parantap Parashar

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

Related Questions