knutsford Posted August 18, 2019 Share Posted August 18, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/ Share on other sites More sharing options...
requinix Posted August 18, 2019 Share Posted August 18, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/#findComment-1569071 Share on other sites More sharing options...
knutsford Posted August 19, 2019 Author Share Posted August 19, 2019 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? Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/#findComment-1569073 Share on other sites More sharing options...
requinix Posted August 19, 2019 Share Posted August 19, 2019 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? Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/#findComment-1569074 Share on other sites More sharing options...
knutsford Posted August 19, 2019 Author Share Posted August 19, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/#findComment-1569075 Share on other sites More sharing options...
knutsford Posted August 19, 2019 Author Share Posted August 19, 2019 You mean I needed the Z in strtotime($date); to make it work? Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/#findComment-1569076 Share on other sites More sharing options...
knutsford Posted August 19, 2019 Author Share Posted August 19, 2019 I had understood that Europe/London took care of BST at at the right time of the year but obviously not Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/#findComment-1569078 Share on other sites More sharing options...
knutsford Posted August 19, 2019 Author Share Posted August 19, 2019 Any way if (date("I", $temp_time)) { $temp_time += 3600; } should work - thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/#findComment-1569079 Share on other sites More sharing options...
Barand Posted August 19, 2019 Share Posted August 19, 2019 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 Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/#findComment-1569080 Share on other sites More sharing options...
requinix Posted August 19, 2019 Share Posted August 19, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/#findComment-1569081 Share on other sites More sharing options...
Barand Posted August 19, 2019 Share Posted August 19, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/#findComment-1569082 Share on other sites More sharing options...
requinix Posted August 19, 2019 Share Posted August 19, 2019 2 minutes ago, Barand said: If he removes the 'Z' timezone indicator My point exactly. Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/#findComment-1569083 Share on other sites More sharing options...
Barand Posted August 19, 2019 Share Posted August 19, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309106-british-summer-time/#findComment-1569084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.