Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

omrakhurs's avatar

How to globally set UK date format in Carbon to auto update for BST

I'd like my app.config to have some sort of setting whereby I could just leave one global setting that makes my dates into British Summer Time when required. Is there a way to make Carbon aware globally of what locale my app is for?

0 likes
8 replies
robgeorgeuk's avatar

Does the below not work?

'timezone' => 'Europe/London'
1 like
omrakhurs's avatar

@robgeorgeuk I've set it to UTC at the moment, should I turn it to Europe/London ? I'm trying to go for a timezone that would automatically adjust for BST.

robgeorgeuk's avatar

Sorry, I think I misread the question. I think the above code will cause all dates (such as the created_at timestamp) to be written in the Europe/London timezone. This might work for your project but I think it's more common to use UTC and then convert when needed using something like:

$timestamp = '2016-05-24 16:34:00';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'Europe/London');
1 like
omrakhurs's avatar

@robgeorgeuk If I were to set it to London/Europe, do you think it would be GMT? It's just so confusing. BST is GMT+1, so if London/Europe is set to GMT zone, then it wouldn't change to BST once the summer time starts. Or would it?

robgeorgeuk's avatar

I'm pretty sure the idea of using named timezones is to automatically take care of these time changes for you.

1 like
ashitvora's avatar

Dealing with Timezones was tedious in one of my apps and the solution I came up with was...

  • Store dates in UTC in the database
  • In View, instead of displaying date, put it in "data-timestamp" attribute of the HTML Element in which you want to display date.
  • Now use MomentJS to loop through all the element containing "data-timestamp" attribute and display Relative (Humanize) time in that HTML Element.
  • I put this is setInterval so it gets invokes every 60 seconds and update the relative time.
  • What I also make sure is that if time difference is more than 24 hrs, I display Day of the Week and if time difference is more than 7 days, I display Date and Month.

This might work in your case as well.

1 like
ian_h's avatar
ian_h
Best Answer
Level 20

I have Europe/London set in my config for my timezone and this seems to be picking up the correct time with no fancy calculations required.

But store all dates as unix timestamps in the database IMO.. it'll save you a whole heap of issues in the future (my worst nightmare was developing a site for Safelite Autoglass a few years ago... UK-based server, US-based company who insisted all dates / times were saved in the datebase in a DateTime field with an EST timezone... argh!!).

Cheers..

Ian

2 likes
omrakhurs's avatar

@ian_h I'm in a similar position. My client has customers based mostly in the UK, but there's a good number who are based in the continent. It would be great if I could just ask MySQL to convert the time from UTC to CET or whatever, as required.

Please or to participate in this conversation.