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');
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have a date:
Mon, 31 Mar 2014 12:19:10 GMT
How can I convert that to this format:
date('Y-m-d H:i:s')
I have found something like this:
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
But how do I convert 'Mar' to '03', so I can use that?
How about using DateTime
$date = new DateTime("Mon, 31 Mar 2014 12:19:10 GMT");
echo $date->format("Y-m-d H:i:s");
No need, PHP's strtotime understands month names.
C:\Users\Niet>php
<?php
var_dump(date('Y-m-d H:i:s',strtotime('Mon, 31 Mar 2014 12:19:10 GMT')));
^Z
string(19) "2014-03-31 12:19:10"
strtotime does the trick here too.
This code:
$date = 'Mon, 31 Mar 2014 12:19:10 GMT';
echo $date;
$time = strtotime('Mon, 31 Mar 2014 12:19:10 GMT');
echo date('Y-m-d H:i:s', $time);
will output this:
Mon, 31 Mar 2014 12:19:10 GMT
2014-03-31 14:19:10
Note that your timezone is important here too. I'm in GMT+2 timezone, so final hour is 14 instead of 12.
In my json response of twitter API I get time stamp like this
Thu Mar 13 14:24:13 +0000 2014
I tried to format in this way:
$created_at = $thing->created_at;
$date = DateTime::createFromFormat('D M d H:m:s O Y', $created_at);
echo $created_at;
echo $date->format('H:m:s');
Which gives result like this:
Thu Mar 13 14:24:13 +0000 2014
2015:12:13 //formated result. How come 2015?????
Wed Mar 12 14:18:14 +0000 2014
2015:06:12
Tue Jan 21 12:50:17 +0000 2014
2018:02:21
Thu Dec 12 09:29:16 +0000 2013
2015:05:12
Why giving wrong result?
I want to get month, year in seperate variable.
You can simplify the creation of the DateTime by doing this:
$dt = new DateTime('#' . strtotime('Thu Mar 13 14:24:13 +0000 2014'));
This parses the date string to a Unix timestamp, and then creates a DateTime object.
echo $dt->format('Y-m-d H:i:s'); // yields the correct result.
You are using month format character m instead of minutes i, thats why you get "wrong" output.
$dt = new DateTime('Thu Mar 13 14:24:13 +0000 2014');
echo $dt->format('H:i:s');
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');
}
I have a date that is passed to PHP as such:
$date = mysql_real_escape_string($_POST["Date"]);
The date displays like this - Wed Sep 10 2013 00:00:00 GMT-0500 (EST)
Output needed - 2013-09-10
$dt = new DateTime('Wed Sep 10 2013 00:00:00 GMT-0500 (EST)');
echo $dt->format('Y-m-d');
See it in action
<?php
// $date = mysql_real_escape_string($_POST["Date"]);
// You should not use mysql_real_escape_string here.
$date = 'Wed Sep 10 2013 00:00:00 GMT-0500 (EST)';
echo date('Y-m-d', strtotime($date));
$date = mysql_real_escape_string($_POST["Date"]);
echo date('Y-m-d', strtotime($date));
Tue Oct 26 10:39:39 +0000 2010
How to convert format to 2010-10-26
echo date('Y-m-d', strtotime("Tue Oct 26 10:39:39 +0000 2010"))
// 2010-10-26
echo date('Y-m-d');
Using the DateTime object:
echo date_format(new DateTime('Tue Oct 26 10:39:39 +0000 2010'), 'Y-m-d');
or:
$date = new DateTime('Tue Oct 26 10:39:39 +0000 2010');
echo $date->format('Y-m-d');