Want to convert GMT date time to IST Date Time - php

i want to convert GMT date time to IST Date Time for that purpose i have tried below code but not getting desired result.
function ConvertGMTToLocalTimezone($gmttime,$timezoneRequired)
{
$system_timezone = date_default_timezone_get();
date_default_timezone_set("GMT");
$gmt = date("Y-m-d h:i:s A");
$local_timezone = $timezoneRequired;
date_default_timezone_set($local_timezone);
$local = date("Y-m-d h:i:s A");
date_default_timezone_set($system_timezone);
$diff = (strtotime($local) - strtotime($gmt));
$date = new DateTime($gmttime);
$date->modify("+$diff seconds");
$timestamp = $date->format("m-d-Y H:i:s");
return $timestamp;
}
$ISTtime=ConvertGMTToLocalTimezone('Tue, 17 Dec 2013 07:23:56 +0000','Asia/Calcutta');
echo $ISTtime;
Result: 12-17-2013 18:34:02
What i am doing wrong?

Why don't you simply do this:
$timestamp = strtotime('Tue, 17 Dec 2013 07:23:56 +0000');
date_default_timezone_set("Asia/Calcutta");
echo date('r', $timestamp);
output
Tue, 17 Dec 2013 12:53:56 +0530

Can you try this, you can use $new = new DateTimeZone('Asia/Kolkata');//IST
$new = new DateTimeZone('Asia/Kolkata');//IST
$date = new DateTime(gmdate("m/d/Y H:i:s"), 'Tue, 17 Dec 2013 07:23:56 +0000');
$date->setTimezone($new);
echo $date->format('m-d-Y H:i:s');

This is a trivial exercise using the dateTime classes:-
function ConvertGMTToLocalTimezone($gmttime,$timezoneRequired)
{
$date = new \DateTime('Tue, 17 Dec 2013 07:23:56 +0000');
$date->setTimeZone(new \DateTimezone($timezoneRequired));
return $date->format('D, d M Y H:i:s O');
}
$ISTtime=ConvertGMTToLocalTimezone('Tue, 17 Dec 2013 07:23:56 +0000','Asia/Calcutta');
echo $ISTtime;
Output:-
Tue, 17 Dec 2013 12:53:56 +0530
See it working
If you have PHP >= 5.4 this will work:-
function ConvertGMTToLocalTimezone($gmttime,$timezoneRequired)
{
return (new \DateTime('Tue, 17 Dec 2013 07:23:56 +0000'))->setTimeZone(new \DateTimezone($timezoneRequired))->format('D, d M Y H:i:s O');
}

Related

how to get this type of date in php like this Date {Fri Jun 24 2016 10:30:06 GMT+0530 (IST)} jquery standerd format

How to get this type of date in PHP like this
Date {Fri Jun 24 2016 10:30:06 GMT+0530 (IST)}
jQuery standerd format.
Try:
Also refer http://php.net/manual/en/function.date.php for different date formats
echo date("D M d Y h:i:s eO T");
Output:
Fri Jun 24 2016 10:52:42 Asia/Calcutta+0530 IST
As you were looking for ยป RFC 2822 formatted date you can simply use
echo date("r"); // Fri, 24 Jun 2016 10:59:36 +0530
Or if you want to be somewhat like as question you can use
echo date("D M d Y H:i:s eO T"); // Fri Jun 24 2016 10:59:37 Asia/Kolkata+0530 IST
Docs
In PHP you can get this format as
<?php
$tzone = new DateTimeZone('Asia/Calcutta');
$dtime = new DateTime("now", $tzone);
echo $dtime->format("D M d Y H:i:s \G\M\TO (T)");
?>
In that case you will get the output:
Fri Jun 24 2016 11:25:40 GMT+0530 (IST)
That is your output as you wanted.
finally got it .... thanks for help..
$tzone = new DateTimeZone('Asia/Calcutta');
$dtime = new DateTime("now", $tzone);
$daet = $dtime->format("D M d Y H:i:s \G\M\TO (T)");
$timestart = explode (' ', microtime());
$timeInSecond = substr($timestart[0],1,4)."Z";
echo preg_replace('/\+.*/',$timeInSecond,date('c',"1466774626"));
//2016-06-24T15:23:46.541Z

Transform date to valid format for PostgreSQL with PHP

I have a string in this format:
Thu, 26 Feb 2015 11:39:59
And I wonder if its possible to transform it to a valid timestamp format in order to insert it in my PostgreSQL database.
So ideally in the end I would have something like:
2015-03-26 11:39:59
Is there a function in PHP to do something like this?
Use DateTime() to format date string.
$date = 'Thu, 26 Feb 2015 11:39:59';
$date = new DateTime($date);
echo $date->format('Y-m-d H:i:s');
You can also use it as DateTime::createFromFormat
$date = 'Thu, 26 Feb 2015 11:39:59';
$date = DateTime::createFromFormat('D, d M Y H:i:s', $date);
echo $date->format('Y-m-d H:i:s'); //2015-03-26 11:39:59

PHP Date and Time and timezone

Is there any php function that would convert a date string to its equivalent Asia/Manila time.
I tried setting Asia Manila as my default timezone but no avail.
Please see sample below that needs to be convert to Asia/Manila Time
Sun, 12 Jan 2015 08:27:42 +0000,
Mon, 12 Jan 2015 00:14:04 -0500,
Mon, 12 Jan 2015 05:13:34 +0000 (UTC),
Mon, 12 Jan 2015 08:57:47 +0000 (UTC),
Tue, 13 Jan 2015 01:38:04 +0700 (WIT),
Tue, 13 Jan 2015 00:47:31 +0900 (JST),
Mon, 12 Jan 2015 23:27:26 +0000
your assistance is highy appreciated..
Thanks in advance.
This is the easiest way
date_default_timezone_set('Asia/Manila'); // Set your default TZ to Asia/Manila
// strtotime() will convert all timezones to your default
echo date('Y-m-d H:i:s' , strtotime('Mon, 12 Jan 2015 05:13:34 +0000 (UTC)'));
Try this
$date = new DateTime('2000-01-01', new DateTimeZone('Asia/Manila'));
echo $date->format('Y-m-d H:i:sP') . "\n";
Please try the following
$sdt = '2012-05-15 10:50:00';
$stz = new DateTimeZone('UTC');
$dtz = new DateTimeZone('Asia/Manila');
$dt = new DateTime($sdt, $stz);
$dt->setTimeZone($dtz);
$ddt = $dt->format('Y-m-d H:i:s');
Hope this wil help you.
function convert_timezone($from_tz, $to_tz, $time = '2008-08-03 12:35:23') {
date_default_timezone_set($from_tz);
$datetime = new DateTime($time);
$time_newTZ = new DateTimeZone($to_tz);
$datetime->setTimezone($time_newTZ);
$newDate = $datetime->format('Y-m-d H:i:s');
return $newDate;
}
echo convert_timezone('Asia/Kuala_Lumpur', 'America/Los_Angeles');

Why is date() returning September 2014 instead of Oct 2014

The $ts returned is correct but the $moyr is returning September 2014
$cal_date = gregoriantojd(10, 1, 2014);
$ts = jdtounix($cal_date);
$moyr = date("F Y", $ts);
echo $moyr;

How to convert yyyy-MM-dd HH:mm:ss to "15th Apr 2010" using PHP

I have the following date format: 2010-04-15 23:59:59
How would I go about converting this into: 15th Apr 2010
Thanks
echo date("jS M Y",strtotime("2010-04-15 23:59:59"));
In addition to using date and strtotime, you can do
$dateTime = new DateTime('2010-04-15 23:59:59');
echo $dateTime->format('jS M Y'); // 15th Apr 2010
or
$dateTime = date_create('2010-04-15 23:59:59');
echo date_format($dateTime, 'jS M Y'); // 15th Apr 2010
$date = date("dS M Y",strtotime("2010-04-15 23:59:59"));
print $date;

Categories