laravel ÅÌÎÊÁ½ÕßÖ®¼ä
ÔÚ laravel ÖУ¬ÅÌÎÊÓï¾äÊÇ laravel ¿ò¼ÜÖÐ×î³£ÓõĹ¦Ð§Ö®Ò»£¬ÓÉÓÚÊý¾ÝÁ¿µÄ²î±ðºÍÅÌÎÊÐèÇóµÄ²î±ð£¬ÎÒÃǾ³£»áÓöµ½ÐèÒªÅÌÎÊÁ½¸öÈÕÆÚÖ®¼ä¼Í¼µÄÇéÐΡ£ËäÈ» laravel ¿ò¼ÜÌṩµÄÅÌÎʹ¹½¨Æ÷ºÜÊÇÇ¿Ê¢£¬µ«ÈôÊÇûÓÐÕÆÎÕÆäÖеÄÇÉÃîÓ÷¨£¬ÎÒÃÇ»áÔÚдÅÌÎÊÓï¾äÀú³ÌÖÐÓöµ½Ò»Ð©À§ÈÅ¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚ laravel ÖÐÅÌÎÊÁ½¸öÈÕÆÚÖ®¼äµÄ¼Í¼¡£
Ê×ÏÈ£¬ÐèÒªÃ÷È·µÄÊÇ£¬ÔÚ Laravel ÖпÉÒÔͨ¹ýÅÌÎʹ¹½¨Æ÷µÄ whereBetween() ÒªÁìʵÏÖ²éÕÒÁ½¸öÈÕÆÚÖ®¼äµÄ¼Í¼¡£whereBetween() ÒªÁì¿É½ÓÊÜÁ½¸ö²ÎÊý£¬¼´×Ö¶ÎÃûºÍÒ»¸öÏòÊý×é¡£ÆäÖУ¬ÏòÊý×éµÄÁ½¸öÔªËØÌåÏÖÁ½¸öÈÕÆÚ¡£
ÀýÈ磬ÔÚÅÌÎÊ users ±íµÄ×¢²áÈÕÆÚÔÚij¸öʱ¼ä¹æÄ£ÄÚµÄÓû§Ê±£¬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺
$users = DB::table('users') ->whereBetween('created_at', ['2021-06-01', '2021-06-30']) ->get();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂë»á·µ»Ø regist_date ÔÚ 2021 Äê 6 Ô·ݵÄÓû§ÐÅÏ¢¡£ÆäÖУ¬created_at ÊÇ users ±íµÄÒ»¸öÈÕÆÚ×ֶΣ¬whereBetween() ÒªÁìÖ¸¶¨Á˸Ã×ֶεÄÈ¡Öµ¹æÄ£¡£get() ÒªÁìÓÃÓÚ»ñÈ¡ÅÌÎÊЧ¹û£¬·µ»ØµÄÊÇÒ»¸ö°üÀ¨ÅÌÎÊЧ¹ûµÄÜöÝÍ£¨Collection£©¹¤¾ß¡£
³ýÁË whereBetween() ÒªÁìÖ®Í⣬ÎÒÃÇ»¹¿ÉÒÔʹÓà whereDate() ÒªÁìÅÌÎÊijһÌìµÄ¼Í¼¡£ÀýÈ磬ÔÚÅÌÎÊ users ±íµÄ×¢²áÈÕÆÚΪijһÌìµÄÓû§Ê±£¬¿ÉÒÔʹÓÃÒÔÏ´úÂ룺
$users = DB::table('users') ->whereDate('created_at', '2021-06-01') ->get();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂë»á·µ»Ø regist_date ¼´ÊÇ 2021 Äê 6 Ô 1 ÈÕµÄÓû§ÐÅÏ¢¡£whereDate() ÒªÁìÖ¸¶¨ÁË created_at ×ֶεÄȡֵΪ 2021 Äê 6 Ô 1 Èյļͼ¡£get() ÒªÁìͬÑùÓÃÓÚ»ñÈ¡ÅÌÎÊЧ¹û¡£
³ýÁË whereBetween() ÒªÁìºÍ whereDate() ÒªÁìÖ®Í⣬Laravel »¹ÌṩÁËÆäËûһЩÅÌÎÊÈÕÆÚµÄÒªÁì¡£ÕâЩҪÁì°üÀ¨ whereDay()¡¢whereMonth()¡¢whereYear()¡¢whereTime()¡¢whereHour() µÈ¡£ÕâЩҪÁìÓÃÓÚÅÌÎÊijһÌ졢ijһÔ¡¢Ä³Ò»Äꡢij¸öʱ¼äµÈÐÅÏ¢¡£Ê¹ÓÃÒªÁìÓë whereBetween() ÒªÁìºÍ whereDate() ÒªÁìÀàËÆ£¬Ôڴ˲»ÔÙ׸Êö¡£
ÐèҪעÖصÄÊÇ£¬Laravel µÄÅÌÎʹ¹½¨Æ÷ÖеÄÈÕÆÚÅÌÎÊÒªÁìĬÈÏÇéÐÎÏÂʹÓõÄÊÇ UTC ʱ¼ä£¬¶ø²»ÊÇÍâµØʱ¼ä¡£ÕâÒâζ×Å£¬ÎÞÂÛÍâµØʱ¼äÊÇʲô£¬ÅÌÎÊʱ¶¼»áƾ֤ UTC ʱ¼ä¾ÙÐÐÅÌËã¡£Òò´Ë£¬ÔÚ¾ÙÐÐÈÕÆÚÅÌÎÊʱ£¬ÐèÈ·±£´«ÈëµÄÈÕÆÚ²ÎÊýÓë UTC ʱ¼äÒ»Ö¡£
ÔÚÏÖʵӦÓÃÖУ¬ÎªÁË×èÖ¹ÓÉÓÚʱÇøÎÊÌâµ¼ÖµÄÅÌÎʹýʧ£¬ÎÒÃÇͨ³£»áÔÚÅÌÎÊÇ°»òÉèÖÃÎļþÖÐָ׼ʱÇø¡£Laravel ÌṩÁËÒ»¸ö¿ÉÒÔÈ«¾ÖÉèÖÃʱÇøµÄ·½·¨£¬¼´ÔÚ config/app.php ÎļþÖÐÉèÖà timezone¡£
ÔÚ Laravel ÖУ¬½«Ê±¼äת»»Îª UTC ʱ¼äµÄÒªÁìÈçÏ£º
$date = new Carbon('2021-06-01 12:00:00', 'Asia/Shanghai'); $utcDate = $date->utc();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂ뽫ÍâµØʱ¼ä ‘2021-06-01 12:00:00’ ת»»ÎªÁË UTC ʱ¼ä²¢¸³Öµ¸ø $utcDate ±äÁ¿¡£Carbon ÊÇ Laravel ÄÚÖõÄÒ»¸öÈÕÆÚʱ¼ä´¦Öóͷ£À࣬ʹÓÃÆðÀ´ºÜÊÇÀû±ã¡£¸ÃÀàÌṩÁ˶àÖÖʱ¼äת»»ÒªÁ죬¿ÉÇáËÉÓ¦¶ÔÖÖÖÖ³¡¾°µÄÐèÇó¡£
×ÛÉÏËùÊö£¬±¾ÎÄÏÈÈÝÁËÔõÑùÔÚ Laravel ÖÐÅÌÎÊÁ½¸öÈÕÆÚÖ®¼äµÄ¼Í¼¡£ÔÚʹÓÃÅÌÎʹ¹½¨Æ÷¾ÙÐÐÈÕÆÚÅÌÎÊʱ£¬Ðè×¢ÖØʱÇøµÄÉèÖã¬ÒÔ¼°´«ÈëµÄÈÕÆÚ²ÎÊýÊÇ·ñÓë UTC ʱ¼äÒ»Ö¡£¶ÔÅÌÎʹ¹½¨Æ÷µÄÎÞаʹÓã¬Äܹ»Ê¹×ðÁú¿Ê±¿ª·¢±äµÃÔ½·¢¾«Á·ºÍ¸ßЧ¡£
ÒÔÉϾÍÊÇlaravel ÅÌÎÊÁ½ÕßÖ®¼äµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡