Jump to content
knutsford

British Summer Time

Recommended Posts

I am having trouble coverting a untix tiem stamp to British Summer time

 

 

$date = substr($row[18], 0, -1);
date_default_timezone_set('Europe/London');
$temp_time = strtotime($date);                        
$local_date = date('Y-m-d H:i', $temp_time);

 

$date has the date in GMT time and I want to take care of the Britsh Summer Time difference but local_date is GMT time as well. I thought date_default_timezone_set('Europe/London'); was supposed to add an hour if it is British Summer Time? How do I take care of BST?

 

Thanks

 

 

 

 

Share this post


Link to post
Share on other sites

If $row[18] does not have any timezone information then parsing the string into a timestamp and formatting the timestamp back into a string will give you the same thing you started with.

Share this post


Link to post
Share on other sites

The date looks like 2019-08-13T13:30:00.000Z. I am confused. I though date_default_timezone_set('Europe/London'); took care of British Summer time and would convert it. What should my code look like then?

Share this post


Link to post
Share on other sites

No. Setting the default timezone sets the default timezone for any operation where PHP needs to know the timezone. If you try to parse a date string that does not have a timezone then PHP will assume it's in the default timezone. What your code is doing is

1. Strip out the part of the date string that contains the timezone
2. Tell PHP that any dates without timezone information should be treated as Europe/London
3. Parse a GMT time string as if it was a GMT/BST time, which will work correctly half of the year but not the other half
4. Format the time back to GMT/BST, giving you what you started with

Given that description, can you tell what you need to do?

Share this post


Link to post
Share on other sites

Ok I have added

                if (date("I", $temp_time))
                {
                    $temp_time += 3600;            
                }

 

before converting it back to a date and time but I didn't think I needed to

Share this post


Link to post
Share on other sites

I had understood that Europe/London took care of BST at at the right time of the year but obviously not

Share this post


Link to post
Share on other sites

Any way

 

if (date("I", $temp_time))
                {
                    $temp_time += 3600;            
                }

 

should work - thanks for your help

Share this post


Link to post
Share on other sites
13 minutes ago, knutsford said:

I had understood that Europe/London took care of BST at at the right time of the year but obviously not

It does...

$tstr = '2019-08-13T13:30:00.000Z';
$dt = new DateTime($tstr);                                // create UTC datetime object
$dt->setTimezone(new DateTimeZone("Europe/London"));      // convert to BST

echo $dt->format('Y-m-d H:i:s P');  //--> 2019-08-13 14:30:00 +01:00

 

Share this post


Link to post
Share on other sites
1 hour ago, knutsford said:

Any way

 

if (date("I", $temp_time))
                {
                    $temp_time += 3600;            
                }

 

should work - thanks for your help

No. The answer was much simpler than that:

2 hours ago, requinix said:

1. Strip out the part of the date string that contains the timezone

That is the cause of the problem, so stop doing it. Barand's code uses DateTime to accomplish the string parsing, timezone, and formatting work, but otherwise is the same as what you have but without the timezone stripping.

Remove the stuff you did with adding seconds, remove the substr() stuff, and if you can also switch to PHP's newer date processing API that Barand demonstrated.

Share this post


Link to post
Share on other sites

If he removes the 'Z' timezone indicator it will assume the timestring is his default timeszone (BST) and not UTC, therefore he will need

$tstr = '2019-08-13T13:30:00.000';
$dt = new DateTime($tstr, new DateTimeZone('UTC'));          // specify UTC timezone

to specify it is a UTC time. Otherwise he's back to where he started as his default is Europe/London already.

Share this post


Link to post
Share on other sites
2 minutes ago, Barand said:

If he removes the 'Z' timezone indicator

My point exactly.

Share this post


Link to post
Share on other sites
21 minutes ago, requinix said:

1. Strip out the part of the date string that contains the timezone

Sorry - misread what you were saying - I thought the above was your instruction on what to to.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.