[英]How to check current date record in laravel?
I am trying to check game_id
only for today or current date and for this I am trying following script 我正在尝试仅检查今天或当前日期的game_id
,为此,我正在尝试遵循脚本
$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). 但是对于game_id=6
存在重复的条目,因为它是3小时前(迪拜时间)生成的。
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. 我只是插入记录,在服务器数据库中显示的是09:31:57
,这意味着是09:31:57
AM,但是我在迪拜,现在这里是1:33 PM。
Can someone kindly guide me about it. 有人可以请我指导一下。
Thank you so much. 非常感谢。
Go to 去
config -> app.php and change 'timezone' => 'Asia/Dubai' 配置-> app.php并更改'timezone'=>'Asia / Dubai'
. 。 Then 然后
php artisan config:cache
您可以在“ config-> app.php”中找到时区配置,只需将'timezone' => 'UTC', to 'timezone' => 'Asia/Dubai',
更改'timezone' => 'UTC', to 'timezone' => 'Asia/Dubai',
然后重新启动XAMPP。
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. 因为您具有mysql服务器时区,lavarel时区设置和计算机的时区设置...当您编写时:Date(created_at)= Curdate(),Curdate()检查mysql服务器的时区。 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 ... ) 请记住,世界上每个用户都有自己的“当前本地日期”,而存储在您的mysql数据库中的日期只会是一个固定日期(最好使用UTC以获得更高的可靠性...)
Laravel 5.6+ users, you can just do Laravel 5.6+用户,您可以做
$posts = Post::whereDate('created_at', Carbon::today())->get();
Besides now()
and today()
, you can also use yesterday()
and tomorrow() and then use the following: 除了now()
和today()
,您还可以使用yesterday()
和明天(),然后使用以下代码:
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. 使用查询生成器,使用Mysql
默认的CURDATE
函数获取当天的所有记录。
$records = DB::table('users')->select(DB::raw('*'))
->whereRaw('Date(created_at) = CURDATE()')->get();
dd($record);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.