5

I'm using the unix timestamp to show when a message was posted in my project but when I came to displaying the exact time of the post I realized it was about 12 hours behind.

I'm in the UK and my server is in the US (might be the problem).

Is there a simple way of converting the unix timestamp into a readable British time?

$timestamp = time();
print date("F jS, Y", strtotime($timestamp));

Any help would be great, Thanks!

6 Answers6

4

At the top of your script, write:

date_default_timezone_set('Europe/London');

Or if your PHP is >= 5.2.0:

date_timezone_set('Europe/London');
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

Just call date_timezone_set with the appropriate parameter for the UK at the start of your script when displaying the dates (not when recording them; I 'm not sure, but it might result in the "wrong" timestamps being recorded).

Edit: The timezone you want is 'Europe/London'.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • I hope not. Otherwise any script running outside of GMT is in big trouble. – Lightness Races in Orbit Apr 24 '11 at 20:24
  • @TomalakGeret'kal: I don't quite get your meaning. I was thinking that the behavior would be dependent on the system clock going with UTC time vs. the local time. In the second case only PHP would need to convert from local to UTC to get a timestamp; therefore setting the "wrong" local timezone would result in a wrong TS. I might be wrong though. – Jon Apr 24 '11 at 20:26
  • Depends what he's doing, I guess. – Lightness Races in Orbit Apr 24 '11 at 20:29
2

try date-default-timezone-set.

date_default_timezone_set('Europe/London');
Alexander Wigmore
  • 3,157
  • 4
  • 38
  • 60
cweinberger
  • 3,518
  • 1
  • 16
  • 31
1

Use date_default_timezone_set('Europe/London'); to set the time zone to London's time. Not sure if it works with summer/winter time.

Alexandre Cassagne
  • 2,384
  • 23
  • 40
0

This one worked for me

date_default_timezone_set('Europe/London');
Szabolcs Páll
  • 1,402
  • 6
  • 25
  • 31
jocar
  • 21
  • 5
0

the simplies way is to substruct gmt offset. e.g:

echo(date('Y-m-d h:i'), $myvalue - 60 * 60 * $nhours));

where $nhours - time defference in hours.

heximal
  • 10,327
  • 5
  • 46
  • 69