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

Deekshith's avatar

Open quiz based on scheduled date and time in laravel

Hi, I have a quiz listing with scheduled date and time like below,

Quiz 1 will open on 28-11-2020 11:00:00 Quiz 2 will open on 29-11-2020 15:00:00

So once scheduled date crosses users will be able to open the quiz at any time.

But the main problem is i have default time zone (date_default_timezone_set("Asia/Kolkata")) in construct function because most of the users are from India.

But if users from other countries tries to access this quiz does he get early or late access , if i set default timezone to INDIA?

0 likes
3 replies
BezhanSalleh's avatar

your best bet is to use UTC as your default timezone... Wiki Weather forecasts and maps all use UTC to avoid confusion about time zones and daylight saving time. ... This ensures that all pilots, regardless of location, are using the same 24-hour clock..

Or if you want you could get each visitors' localtime and adjust it accordingly.

--Helper function...
if(! function_exists('getVisitorsLocalTime')) {
        function getVisitorsLocalTime(){
    
            $ip = file_get_contents("http://ipecho.net/plain");
            $url = 'http://ip-api.com/json/'.$ip;
            $tz = file_get_contents($url);

            return  json_decode($tz,true)['timezone'];
        }
 }

now you can use the above helper function to get each visitor's local time and compare it with your quizez open time and adjust accordingly...

 //time based on Visitor/User of your website's timezone
Carbon::now(getVisitorsLocalTime());

//time based on your application's timezone
Carbon::now()
Deekshith's avatar

@bezhansalleh so consider i have a test link scheduled date like below,

2020-11-30 this means test will open exactly 12AM. consider now current date and time in india is 2020-11-30 12 AM then indian users can able to access this test link. Now if user is in different country with current date time 2020-11-29 8 PM and he will get access to schedule test link because in india it crosses 12AM. even though if he tries to access the test at this time he falls in the same category of indian users so i don't think there is problem with this right? so every user will take test at same time but in their regions. this is how it will happen right?

Please or to participate in this conversation.