2

I am trying to check game_id only for today or current date and for this I am trying following script

$todayExist = Training::whereRaw("Date(created_at)=Curdate() and game_id=$game_id")->get();
        if($todayExist->count() > 0 ){
            $error = 1;
            $result = false;
        } 

The above query output is

select * from `games_training` where Date(created_at)=Curdate() and game_id=6

But for game_id=6 there is duplicate entry, as it was generated by 3 hours ago (Dubai Time).

So can someone kindly guide me what can be the issue? Is it wrong query or it happened because server timezone.

I just insert record, in server database it is showing 09:31:57 which mean is 09:31 AM, however I am in Dubai and right now here is 1:33 PM.

Can someone kindly guide me about it.

Thank you so much.

2
  • 1
    09:31 is UTC time. check UTC current time you'll get, Default timezone is UTC in laravel. Commented Sep 4, 2019 at 9:37
  • 1
    you should use Asia/Dubai for timezone Commented Sep 4, 2019 at 9:45

4 Answers 4

2

Laravel 5.6+ users, you can just do

$posts = Post::whereDate('created_at', Carbon::today())->get();

Besides now() and today(), you can also use yesterday() and tomorrow() and then use the following:

startOfDay()/endOfDay()
startOfWeek()/endOfWeek()
startOfMonth()/endOfMonth()
startOfYear()/endOfYear()
startOfDecade()/endOfDecade()
startOfCentury()/endOfCentury()

OR

Using query builder,Use Mysql default CURDATE function to get all the records of the day.

 $records = DB::table('users')->select(DB::raw('*'))
                  ->whereRaw('Date(created_at) = CURDATE()')->get();
    dd($record);
Sign up to request clarification or add additional context in comments.

Comments

0

Go to

config -> app.php and change 'timezone' => 'Asia/Dubai'

. Then

php artisan config:cache

Comments

0

You can find the timezone configuration in "config -> app.php" just change 'timezone' => 'UTC', to 'timezone' => 'Asia/Dubai',and restart your XAMPP.

3 Comments

You'll be better off using UTC in the DB and doing the timezone translation in the application.
If your application had launched in many location with different time zone then you can have your application configuration according to that specific time zone. It will help you to manage your date and time calculation.
Yes, and it's better to do that in the application, rather than the database. See stackoverflow.com/questions/11537106/… and blog.abodit.com/2010/02/….
0

The current date means what for you? Because you have the mysql server timezone, your lavarel timezone setting and your computer's timezone setting ... When you write: Date (created_at) = Curdate (), Curdate () examines the time zone of the mysql server. So what is the "current date" you want to find ? Remember that every user in the world has their own "current local date" while the date stored in your mysql database will only be a fixed date (it is better to be UTC for more reliability ... )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.