1

I am using facebook apis and getting created_time as 2012-06-06T16:20:43+0000

I have posted this status at about 21:57 (IST)

and i am getting 2012-06-06T16:20:43+0000

any help will be appreciated, to get the same time as i have posted to get the exact updates.

4 Answers4

2

you can convert any timezone using this code:

$timestamp = strtotime("2012-06-06T16:20:43+0000");   //here you put your string with the tie

$dtime = new DateTime();
$dtime->setTimestamp($timestamp);
$localtz = new DateTimeZone("Asia/Calcutta");         //choose the correct PHP timezone
$dtime->setTimeZone($localtz);                        //we apply the timezone

$stringtime = $dtime->format('Y-m-d\TH:i:sO');        //here you return the time in the same format as facebook
$unixtime = $dtime->format('U');                      //here u get the unix timestamp format of the same string

print $stringtime;
Luca S.
  • 94
  • 1
  • 9
  • Hello Luca, Thanks for your valueable feedback. But I tried above code. It displays nothing. can you please check? Thanks – Softprodigy India Jun 07 '12 at 12:15
  • To know the argoment of DateTimeZone constructor, click on one of the links at [http://php.net/manual/en/timezones.php ]. – Fil Nov 04 '17 at 00:03
1

I've written a function earlier for this task,

I've updated it for your needs,

var fbDateFix = function(date){ 
var local = new Date(date.replace(/-/g,'/').replace('T',' ').replace('+0000',''));
local.setSeconds(local.getSeconds() + 19800);
return local;
}
var padZero = function(t){
  if(t<10){
     return '0' + t;
  }
  return t;
}
var d = fbDateFix('2012-06-06T16:20:43+0000');
console.log(d);
var month = padZero(+d.getMonth()+1);
var date = padZero(+d.getDate());
var hour = padZero(+d.getHours());
var min = padZero (+d.getMinutes());
var sec = padZero (+d.getSeconds());
console.log(d.getFullYear() + '-' + month + '-' + date + 'T' + hour + ':' + min + ':' + sec + '+0000')

Edit:

This works for me in php,

date_default_timezone_set('Asia/Calcutta');
$timestamp = strtotime('2012-06-06T16:20:43+0000');
$local_datetime = date('c',$timestamp); 
echo $local_datetime;
Jashwant
  • 28,410
  • 16
  • 70
  • 105
  • Hello Jashwant, Thanks for your valueable reply. But it is displaying wrong year and date. Also we need the result in same output format. It is currently displaying:- 2012-06-07T06:51:32+0000 as Mon Jan 08 2035 12:21:32 GMT+0530 (IST) can you please check it? Thanks – Softprodigy India Jun 07 '12 at 07:20
  • Hello Jashwant, Is it possible to do all this using php. Because I have to manipulate this time in php in order to show in graph. Thanks – Softprodigy India Jun 07 '12 at 10:13
  • @SoftprodigyIndia, I've edited my js code and also added the php code. I am always consfused when dealing with datetime ;) So, please check it on your side :) – Jashwant Jun 07 '12 at 11:03
  • Hello Jashwant, I have used your php code date_default_timezone_set('Asia/Calcutta'); $timestamp = strtotime('2012-06-06T16:20:43+0000'); $local_datetime = date('c',$timestamp); echo $local_datetime; I have used date_default_timezone_get() function to get user's timezone. It returned "UTC". I think code doesn't work for UTC. Is it? I want to use date_default_timezone_get() function to get users timezone and change time accordingly. Your code works well if I use "Asia/Calcutta" but if I use date_default_timezone_get() i.e. it returns "UTC". then code doesn't work. Thanks – Softprodigy India Jun 07 '12 at 13:44
  • You cannot get user's timezone by php. Php resides on server and will always given timezone on server. Either ask user about his/her timezone or use javascript. `new Date().getTimezoneOffset()/60` will give the timezone to you. – Jashwant Jun 07 '12 at 13:58
  • I recommend asking user about his/her timezone. Thats the best and standard way. Also, mark my answer as answer if it helps you :) – Jashwant Jun 07 '12 at 14:04
  • Hello Jashwant, is it possible to put javascript variable value in php variable without using cookie, form etc? I am thinking about getting correct time with javascript then pass it to php variable. Thanks – Softprodigy India Jun 07 '12 at 15:08
  • Can you please tell me the process. – Softprodigy India Jun 07 '12 at 15:41
  • [jQuery Ajax](http://api.jquery.com/jQuery.ajax/) will help. Try coding yourself. If that doesnt help. Ask a different question regarding that. Avoid discussion here :) – Jashwant Jun 07 '12 at 17:08
0

It looks like the date returned to you is a UTC datetime. You need to convert it to your local timezone (IST)...

See the following SO question for converting datetimes using php: php convert datetime to UTC

Community
  • 1
  • 1
Seth Flowers
  • 8,990
  • 2
  • 29
  • 42
-1

You can use PHP's strtotime to convert created_time to a UNIX timestamp. Then you can add or subtract your time zone offset (IST is UTC+05:30).

sachleen
  • 30,730
  • 8
  • 78
  • 73
  • You don’t add or subtract from Unix timestamps, that’d be just plain stupid (sorry). What you do instead is, you _format_ the unix timestamp according to your timezone. Set the default timezone in your PHP config, and date() will display in that timezone – that’s all there is to it. – CBroe Jun 06 '12 at 17:17
  • I understand it would make a difference if you had to add or subtract longer periods of time (you'd have to account for leap years and stuff) but for 5 hours, why would subtracting 18000 seconds be wrong? – sachleen Jun 06 '12 at 18:16
  • F.e., because some countries have daylight saving time, others don’t, and those who do switch to/from it on different dates … It’s just a thing you _don’t_ do, especially since it’s a) absolutly unneccessary, and b) also **wrong**. Your timestamp with x seconds added/subtracted represents a completely different point in time, no matter where you are in the world. Later on you might get it into your head to pass on this wrong timestamp, which may look fine in your application, to someone else, and then they try to format it correctly regarding their timezone, and things start getting messy … – CBroe Jun 06 '12 at 18:22