This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

shoguniphicus
shoguniphicus

I usually leave the timezone as 'UTC' as it served most of my projects well. But for one particular project, timezone is important because it's being used to show schedule showtime of a channel.

I set mine to "Asia/Kuala_Lumpur", and the recorded saved in MySQL is always +8 hours of the timestamp that I intend to save. The backend fields seems to intelligently corrected the timestamp saved in MySQL to display correctly. For example, if my time now is 12pm, the data saved in MySQL would be 8pm, but the field would still show 12pm. Which is fine by me. I think that's the way the system is designed to work.

What didnt work for me, however, is when I tried to query the record, say if I wanted to get record between certain hours using Carbon:now(), then this would mess me up because Carbon also have take into account of the current timezone and gives me my exact hours of my timezone. So what I do now is that I manually add +8 hours to my query and that gives me the result that I want.

I want to ask if what I am doing is right? because this feels counter-intuitive at best.

brianwachira758369
brianwachira758369

shoguniphicus said:

I usually leave the timezone as 'UTC' as it served most of my projects well. But for one particular project, timezone is important because it's being used to show schedule showtime of a channel.

I set mine to "Asia/Kuala_Lumpur", and the recorded saved in MySQL is always +8 hours of the timestamp that I intend to save. The backend fields seems to intelligently corrected the timestamp saved in MySQL to display correctly. For example, if my time now is 12pm, the data saved in MySQL would be 8pm, but the field would still show 12pm. Which is fine by me. I think that's the way the system is designed to work.

What didnt work for me, however, is when I tried to query the record, say if I wanted to get record between certain hours using Carbon:now(), then this would mess me up because Carbon also have take into account of the current timezone and gives me my exact hours of my timezone. So what I do now is that I manually add +8 hours to my query and that gives me the result that I want.

I want to ask if what I am doing is right? because this feels counter-intuitive at best.

Have you found a solution? So I am also experiencing the same issue when using rainlab blog plugin to save my blog posts. The articles get saved but they are not being displayed till after 3 hours of the publishing time. I changed the timezone in config/app.php to my city. Posts published before 12 noon get displayed but any post published after 12 noon is not shown

Last updated

mjauvin
mjauvin

leave timezone in config/app.php set to UTC and change the one in config/cms.php

KurtJensen
KurtJensen

Just struggled with something similar. Here was my final setup. Shared server is running on "MST" as was MySQL by default

Config Files:

app.php

...
'timezone'=>'UTC',
...

database.php

...
'mysql'=>[
    'timezone'=>'-00:00',
    'driver'=> ...
]

Backend: user can pick their timezone in their individual preferences. I chose 'UTC' for checking that everything was time stamping correctly.

Frontend: Need to convert to users TZ for presentation and forms. Your save process must convert from User TZ to 'UTC'. You can use JS to get the user TZ from browser and AJAX it to your server for saving a session variable or other method of telling server what TZ the user is using.

<script>
tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
window.addEventListener("load", function(){
$.request('onSetTZ', { data: {tz: tz}});
});
</script>

You can convert TZ in twig but I suggest that you handle all TZ conversions in PHP using Carbon for retrieval, presentation and saving because it will be easier if this is done all at the same layer.

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.