views:

77

answers:

1

I just got some tweets using their XML feed, and have gotten the date/times like so

Tue Sep 29 22:30:51 +0000 2009

I tried using strtotime() on this, but it wasn't producing the correct results.

I later on use

echo date($dateTimeFormat, $twitterUnixTime);

I know I could use a regex, but from the looks of it it will be complicated (and will need to use a callback, to convert 'Sep' to a numeral. From what I understand, strtotime() can easily do MySQL dates (2009-09-30 13:00:00) and some others (generally from RSS feeds).

What can I do here to get this date/time as an Unix Epoch time?

UPDATE

Here is my code

$dateTime = $status->created_at; // Tue Sep 29 22:30:51 +0000 2009

$date = strtotime($dateTime);

$dateTimeFormat = 'j/n/y g:ia';

echo date($dateTimeFormat, $date); // 2/2/09 12:01am

UPDATE

Sorry guys, entirely my fault. I have a variable mistyped. Thanks for your answers!

+3  A: 

Hi,

What is exactly the problem you are getting with strtotime ?

Using this code :

$str = 'Tue Sep 29 22:30:51 +0000 2009';
$ts = strtotime($str);

var_dump(date('Y-m-d H:i:s', $ts));

I get this output :

string '2009-09-30 00:30:51' (length=19)

So, wrong by 2 hours -- is that the kind of problem you have ?

If yes, it might be a problem of timezone : I am in France, and there is a difference of 2 hours with GMT/UTC time.


Changing locale with the date_default_timezone_set function (to set one that is in the GMT timezone), with this line before the previous ones :

date_default_timezone_set('Europe/London');

Gets this as output :

string '2009-09-29 23:30:51' (length=19)

Which kind of look like the desired result ?


You might also change the date.timezone setting in php.ini ; but might have some other impacts, so not sure it's that much of a good idea...

Maybe you could use the "UTC" timezone, btw, like this :

date_default_timezone_set('UTC');

Considering what we are willing to get/mean, it might actually be a better idea...

Pascal MARTIN
You can also use `gmdate()` to get the GMT/UTC date without changing timezones.
too much php
"So, wrong by 2 hours?" that's not wrong at all, it's showing the exact same moment in time in your timezone
nickf
by "wrong", I meant "not what you might expect", actually -- thanks about gmdate !
Pascal MARTIN