4

I'm using the Envato API to verify purchases made on their site, and the date/time outputted as part of an API query for the purchase is like so:

Wed May 20 06:37:57 +1000 2015

Firstly, what format is this in (is there a name for it, and also in terms of the PHP date/time format?) and secondly how can I convert this into a different format?

When I use the strtotime() function and then date() as below, the output seems to be wrong by a few hours. Could this be because of a different in time zones between Envato (Australia) and me (England) or am I missing something?

$old = strtotime("Wed May 20 06:37:57 +1000 2015");
echo date('Y-m-d H:i:s', $old);

The above outputs the following: 2015-05-19 22:37:57.

Any help would be much appreciated, thanks!

Sam B.
  • 69
  • 8
  • 2
    I don't know, but i feel like you can directly create a new date object from the date format.. And then print it formatted like you want to.. perhaps like such? http://sandbox.onlinephpfunctions.com/code/0a1d2686d74801eb4422f5845277c3ee1d444925 ( Output : 2015-05-20 06:37:57 ). Well presented question anyway, +1. Be aware that if you change the timezone you're going to do that for every single date.. While if you create a datetime object it will work properly on that date specifically. – briosheje May 27 '15 at 10:16
  • @briosheje - great, thanks. If I don't specify a timezone (as in my original post), would that mean that PHP is auto converting it from Australian time to UK time somehow? I'm confused as to what the original code was doing to change it from May 20th to May 19th, and the time (hours) being incorrect too. – Sam B. May 27 '15 at 10:24
  • 1
    Instead of crossing your fingers and hoping `strtotime` magically figures out the format, you should listen to briosheje's recommendation - use `DateTime::createFromFormat(...)` and specify the input format explicitly. As a bonus, you'll get a full-fledged and timezone-aware date object to work with, instead of just a timestamp. – DCoder May 27 '15 at 10:24
  • 1
    As for the time change you're seeing, your input contains `+1000`, which is a timezone specifier for "*GMT+10 hours*", and strtotime figures it out as such. – DCoder May 27 '15 at 10:26
  • @SamBerson : In a nutshell it is converting it to UK time, because in fact your timezone is the UK one (so it makes sense). The problems that might occur when switching timezone are: 1) They are strictly related to the OS : I've seen strange things happening in windows lately. 2) You are PERMANENTLY changing the timezone, so every single date you are printing later WILL be an "australian date", unless you change the timezone again. So, perhaps, you just want to specifically create a date object for THAT single date, preserving the rest of the dates as they currently are. – briosheje May 27 '15 at 10:27
  • This is great, thank you @DCoder! If I want to tell PHP that this is in ACT and then switch it to GMT, how would I go about doing that within the object so not all other dates get necessarily affected? @briosheje – Sam B. May 27 '15 at 10:27
  • (Also, as a side note, you don't always know exactly where your server is.. If you're trying that locally you know where you are, but if you are using an external host that may someday migrate your files in another server you may find yourself screwed if you don't create the datetime object :P) – briosheje May 27 '15 at 10:31
  • `$old = DateTime::createFromFormat("D M d H:i:s P Y", "Wed May 20 06:37:57 +1000 2015"); echo date_format($old, 'Y-m-d H:i:s');` - that's what I have so far. So, knowing that the original is in Australian time, how can I convert the output into GMT (UK time) but within the object? @DCoder @briosheje – Sam B. May 27 '15 at 10:34
  • 2
    [`$old->setTimezone(new DateTimeZone('Europe/London'));`](https://php.net/datetime.settimezone), followed by [`echo $old->format('Y-m-d H:i:s');`](https://php.net/datetime.format) . It's in the documentation. – DCoder May 27 '15 at 10:38

2 Answers2

3

this should work :

$date="Wed May 20 06:37:57 +1000 2015";
$datetime = DateTime::createFromFormat('D F d H:i:s O Y',$date);
echo $datetime->format('Y-m-d H:i:s');

and if you want to convert-it to british timezone :

$datetime->setTimezone(new DateTimezone('Europe/London'));
echo "Queen's datetime: ".$datetime->format('Y-m-d H:i:s')
n00dl3
  • 21,213
  • 7
  • 66
  • 76
2

Try this one

date_default_timezone_set('Australia/ACT');
$old = strtotime("Wed May 20 06:37:57 +1000 2015");
echo date('Y-m-d H:i:s', $old);
Alghi Fari
  • 440
  • 3
  • 12
  • Thanks, so to clarify, this retains the timezone so that everything here is in Australian time? If I want to convert it to GMT/UK time, I simple leave out that line (which is what was happening before)? – Sam B. May 27 '15 at 10:22
  • if you want to convert to GMT/UK time change timezone_set. so time will convert to UK time. the good choice is insert to databse date time and timezone or make one timezone default for all setting. – Alghi Fari May 27 '15 at 10:31