How to convert GMT time to IST? - php

Here I am getting GMT Time as follow.
1520489880
Now I am getting This as
$myfinaldate = gmdate("d M H:i", $strtoDate);
So it will returns me GMT time. Now i want to convet it to IST(indian standard time).
where I stuck when I try
//Set timezone to india
date_default_timezone_set('Asia/Kolkata');
echo date('Y-m-d H:i:s');
But this returns me current time not the time that I get in strtotime

You need to pass the timestamp:
date_default_timezone_set('Asia/Kolkata');
echo date('d M H:i', $strToDate); // where $strToDate is 1520489880
Would produce the following value:
08 Mar 11:48

You can convert GMT time to IST time using following ways :
1.
echo date('Y-m-d H:i:s',strtotime('+330 minutes', 0));
2.
date_default_timezone_set('Asia/Kolkata');
echo date('Y-m-d H:i:s');

Changing default timezone is not such a good idea. Use DateTime class, any you don't need to worry about default timezone anymore...
$dt = new DateTime("#$strToDate");
$dt->setTimezone(new DateTimezone('Asia/Kolkata'));
echo $dt->format('Y-m-d H:i:s');
demo

Related

date() and gmdate() return different results

I run a simple test comparing output of date() and gmdate() and I'm puzzled with results. I understand the difference between functions, however, the server is set to GMT time so I would expect the result to be identical.
I checked timezone setting and they seem to be fine. I suspect this has something to do with daytime savings.
I am right to expect the output to be the same?
if (date_default_timezone_get()) {
echo 'date_default_timezone_set: ' . date_default_timezone_get() . '<br />';
}
echo '<br>Full date '.date("Y-m-d H:i:s T I");
echo '<br>Full date GM '.gmdate("Y-m-d H:i:s T I");
$now = date("Y-m-d H:i:s T I");
$tempDate = $startWeek = time();
$date = new DateTime();
$tempDateU = $date->format('U');
$tempDate = $startWeek = time();
echo '<br>Date: '.date("d/m/y H:i", $tempDate);
echo '<br>GM Date: '.gmdate("d/m/y H:i", $tempDate);
echo '<br>Date: '.date("d/m/y H:i", $tempDateU);
echo '<br>GM Date: '.gmdate("d/m/y H:i", $tempDateU);
Output:
date_default_timezone_set: Europe/London
Full date 2016-04-29 11:35:55 BST 1
Full date GM 2016-04-29 10:35:55 GMT 0
Date: 29/04/16 11:35
GM Date: 29/04/16 10:35
Date: 29/04/16 11:35
GM Date: 29/04/16 10:35
*Warning: date_default_timezone_get(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function.*
So you need to set your current timezone to be sure what you are doing:
date_default_timezone_set('Europe/London');
Or
date_default_timezone_set('UTC');
Like mentioned before, Europe/London is not the same as GMT/UTC.
Reminder: GMT does not have DST, it is always the same time.
Europe/London has DST. That is why I in gmdate gives 0
Yes, daylight saving is causing this.
Hence the date() says BST 1 and gmdate() says GMT 0.
Not sure why though, will look into it. Possibly as the time zone is London, so it's in BST.

php code not converting gmt to ist properly

This is a very small issue yet i am not able to solve it
i wish to convert gmt to ist through php, for this i have the following code
$timestamp = strtotime('Tue, 17 Dec 2013 07:23:56 +0000');
date_default_timezone_set("Asia/Calcutta");
echo date('r', $timestamp);
the code is working properly, now i have an array through which i have fetched time which is in this format
10-Dec-2015 10:45:02 +0000
i have stored in a variable $maildate
Now when i am placing this variable in the code like this
$timestamp = strtotime('$maildate');
date_default_timezone_set("Asia/Calcutta");
echo date('r', $timestamp);
i am getting this date and time Thu, 01 Jan 1970 05:30:00 +0530
can anyone please tell where i am going wrong
You need to get rid of quotes and update your code from
$timestamp = strtotime('$maildate');
^^ ^^
into
$timestamp = strtotime($maildate);
//^^ ^^ Removed quotes
Instead you can use DateTime class like as
$date = DateTime::createFromFormat('d-M-Y H:i:s P',$var);
$date->setTimeZone(new DateTimeZone('Asia/Kolkata'));
echo $date->format('r');
Check out this
$timestamp = strtotime($maildate);
date_default_timezone_set("Asia/Calcutta");
echo date('r', $timestamp);

How to Convert PST to GMT time in php?

I would like convert PST to GMT.
Ex:
PST : 22:00 need to convert in to GMT. I have to consider about DAY LIGHT SAVING TIME month also.
How can i do that?
Use PHP's built-in DateTime class...
Use DateTime objects which have this functionality built in:
$date = new DateTime('2013-08-06 15:00:00', new DateTimeZone('America/Los_Angeles'));
echo "The time in Los Angeles is " . $date->format('Y-m-d H:i:s') . "<br>";
$date->setTimezone(new DateTimeZone('Europe/London'));
echo "The time in London is " . $date->format('Y-m-d H:i:s') . "<br>";
(Example Code) (Full Documentation)
I have to do similar for one my work and here it is how approached this:
//date time in PST
$t = 'Tue Feb 04 23:02:09 PST 2014';
$dateTime = DateTime::createFromFormat("D M d H:i:s \P\S\T Y", $t, (new DateTimeZone('America/Los_Angeles')));
var_dump($dateTime); // date time in PST format
$dateTime->setTimezone(new DateTimeZone('UTC'));
var_dump($dateTime); // date time in GMT format

Linux timestamp to PHP

I have the following timestamp:
1342259667654
which when converted with http://www.epochconverter.com/ gives:
Assuming that this timestamp is in milliseconds:
GMT: Sat, 14 Jul 2012 09:54:27 GMT
Your time zone: 14. juli 2012 11:54:27 GMT+2
And that is the correct time, but when using:
echo date("Y-m-d H:i:s", 1342259667654);
I get the following date:
1904-07-24 10:22:47
How can I get with PHP the exact date out of this time stamp?
Your timestamp needs to be divided by 1000:
echo date("Y-m-d H:i:s", 1342259667654/1000);
$timestamp = 1342259667;
$dt = new DateTime("#$timestamp"); // convert UNIX timestamp to PHP DateTime
echo $dt->format('Y-m-d H:i:s');
You can also do it this way.
The value 1342259667654 is actually in miliseconds, while PHP's date() function is unable to handle miliseconds value. Hence the weird output.

how to convert php date formats to GMT and vice versa?

i am new to php.
i want to write a function where i need user to input date in any date format including DST,into GMT format and again later back into the original entered format.please any body help me.
Although the gmdate functions are available. If you are using PHP 5.2 or greater, then consider using the DateTime object.
Here's code to switch to GMT
$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT'));
and back to the default timezone...
$date = new DateTime('2011-01-01', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));
Using the DateTime object lets your create a datetime, just like the procedural functions, except that you keep a reference to an instance.
e.g.
// Get a reference to Christmas of 2011, at lunch time.
$date = new DateTime('2011-12-25 13:00:00');
// Print the date for people to see, in whatever format we specify.
echo $date->format('D jS M y');
// Change the timezone to GMT.
$date->setTimezone(new DateTimeZone('GMT'));
// Now print the date/time it would in the GMT timezone
// as opposed to the default timezone it was created with.
echo $date->format('Y-m-d H:i:s');
// Just to show of some more, get the previous Sunday
$date->modify('previous Sunday');
There's a whole lot of functions you can use, that are much more readable that the procedural functions.
Explicit example of converting from a timezone to GMT
$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');
$date = new DateTime('2011-12-25 00:00:00', $melbourne);
$date->setTimezone($gmt);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.
echo '<br/>';
// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');
Using your Asia/Kolkata to America/New_York
date_default_timezone_set('Asia/Kolkata');
$date = new DateTime('2011-03-28 13:00:00');
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format("Y-m-d H:i:s");
//Outputs: 2011-03-28 03:30:00
Use the gmdate function to convert to GMT time.
For example
$d = '2011-03-28 12:05:20';
$gmt = gmdate('Y-m-d H:i:s',strtotime($d));
// Convert local time to gmt
public function convertTime($timezone,$time){
$selectedtime = date("Y-m-d H:i",strtotime($time));
$date = new DateTime($selectedtime, new DateTimeZone($timezone));
$date->setTimezone(new DateTimeZone('GMT'));
$convertedtime = strtotime($date->format('Y-m-d H:i'));
return $convertedtime;
}

Categories