Hi,
I need to put a time on my website which simply shows the current GMT London time...

I tried using <? echo date("g:ia") ?> but for some reason it displays the time exactly one hour ahead of my time. It should be displaying Europe/London time. (I do live in the London time zone)
I've also tried gmtime(); etc etc. but nothing seems to work.


Does anyone know how I should do this?

I'd really appreciate some help.

Martin

Dani AI

Generated

A short, practical clarification that ties the thread together: saw a time one hour out because the server and PHP timezone handling didn’t match the intended London clock. ’s offset math and ’s manual -3600 fix both correct simple cases but are brittle, and ’s midnight/date glitch is exactly the kind of bug that comes from doing arithmetic on timestamps. was right to point at setting the timezone, but choosing GMT forces UTC year‑round and won’t show British Summer Time.

The robust, maintainable solution is to use PHP’s timezone-aware DateTime API with the IANA identifier for London so DST and date rollovers are handled automatically. Example:

$dt = new DateTime('now', new DateTimeZone('Europe/London'));
echo $dt->format('g:ia');

That outputs the correct local clock for London and avoids manual +/−3600 fixes. If the intent is always true GMT/UTC (no DST), construct the DateTime with the 'UTC' zone instead.

Operational tips: set php.ini’s date.timezone to "Europe/London" and restart the server so library functions match the expected zone; verify with phpinfo() or date_default_timezone_get(). Avoid hard-coded hourly offsets — they fail on DST transitions and around midnight and create the exact date-change issues reported earlier.

are you trying to display the date or time? or both?

Have you tried a simple output??
<?php
echo date("m/d/y");
?>

If that doesnt work try this:

//get the current server date as timestamp
$dat = mktime(date("G"), date("i"), date("s"), date("n"), date("j"), date("Y"));
//work out seconds difference
$difference = substr(date("O",$dat),1,2);
$datdif = 60 * 60 * $difference;
//add or subtract seconds where necessary
if (substr(date("O",$dat),0,1) == '+') {
$dat = $dat - $datdif;
}
else {
$dat = $dat + $datdif;
}
//output date in hh:mm:ss format
echo date("H:i:s", $dat);
?>

If these dont work let me know...theres a few more ways to try.

Thank you :-)

I've been struggling with that for so long :->

I've simplified it a bit as well so that it can just subtract one hour from the current timestamp (if this is any help to anyone else) :

<?
//get current timestamp and subtract one hour
$dat = time() - 3600;
//output time
echo date("g:ia", $dat);
?>

Thanks again,
Martin

I tried the sample code, and it does make the time show correctly, one hour behind. But when it gets to 11:00 pm, it changes the date and causes problems. Is there a foolproof way to solve this?

Thank you.

use this:

date_default_timezone_set('GMT');

Tested and works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.