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.

Imran Abbas
  • 755
  • 1
  • 7
  • 24

4 Answers4

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);
sss S
  • 444
  • 2
  • 7
0

Go to

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

. Then

php artisan config:cache

farooq
  • 1,603
  • 2
  • 17
  • 33
0

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

  • You'll be better off using UTC in the DB and doing the timezone translation in the application. – ceejayoz Sep 04 '19 at 14:12
  • 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. – CHARITRA SHRESTHA Sep 05 '19 at 16:13
  • Yes, and it's better to do that in the application, rather than the database. See https://stackoverflow.com/questions/11537106/is-it-always-a-good-idea-to-store-time-in-utc-or-is-this-the-case-where-storing and http://blog.abodit.com/2010/02/datetime-values-should-always-be-stored-in-utc/. – ceejayoz Sep 05 '19 at 16:15
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 ... )

HRD
  • 51
  • 1
  • 3