ÔõÑùÔÚLaravelÖÐÉèÖÃʱ¼ä
laravelÊÇÒ»¸öÊ¢ÐеÄphp¿ò¼Ü£¬ÓÃÓÚwebÓ¦ÓóÌÐò¿ª·¢¡£Ê±¼ä´¦ÀíÊÇwebÓ¦ÓóÌÐòÖÐÖ÷ÒªµÄÒ»²¿·Ö£¬ÓÉÓÚËüÃÇÐèÒªÏÔʾºÍ´¦ÀíһЩÈÕÆÚºÍʱ¼ä¡£
Laravel¾ßÓÐÄÚÖõÄʱ¼ä´¦Àí¹¦Ð§£¬Ê¹µÃʱ¼ä´¦ÀíºÜÊÇÈÝÒס£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃǽ«ÏàʶÔõÑùÔÚLaravelÖÐÉèÖÃʱ¼ä¡£
ϵͳʱÇøÉèÖÃ
ÔÚLaravelÖУ¬ÎÒÃÇÐèÒªÉèÖÃϵͳʱÇøÒÔÈ·±£×¼È·µÄʱ¼ä´¦Àí¡£Ä¬ÈÏÇéÐÎÏ£¬LaravelʹÓÃUTC£¨Ðµ÷ÌìÏÂʱ£©×÷ΪϵͳʱÇø¡£¿ÉÊÇ£¬Äú¿ÉÒÔƾ֤ÐèÒª¸ü¸ÄËü¡£
ÔÚconfig/app.phpÎļþÖУ¬¿ÉÒÔÕÒµ½¸ÃÓ¦ÓóÌÐòµÄʱÇøÉèÖá£Äú¿ÉÒÔ¸ü¸ÄʱÇøÉèÖÃÒÔÈ·±£Ó¦ÓóÌÐò·´Ó¦×¼È·µÄʱÇø¡£ÒÔÏÂÊÇÉèÖÃʱÇøµÄʾÀý£º
'timezone' => 'Asia/Shanghai',
µÇ¼ºó¸´ÖÆ
Õ⽫°ÑʱÇøÉèÖÃΪÖйú±ê׼ʱ¼ä¡£
ÈÕÆÚºÍʱ¼äÃûÌÃ
LaravelÖ§³Ö¶àÖÖÈÕÆÚºÍʱ¼äÃûÌá£Äú¿ÉÒÔƾ֤ÐèÒªÉèÖÃÈÕÆÚºÍʱ¼äÃûÌá£
ÔÚÓ¦ÓóÌÐòÖУ¬Äú¿ÉÒÔʹÓÃCarbonÀàÀ´´¦ÀíÈÕÆÚºÍʱ¼ä¡£CarbonÀàÊÇPHP DateTimeÀàµÄÒ»¸ö°ü×°Æ÷¡£ËüÌṩÁËÐí¶àÓÐÓõÄÒªÁìÀ´ÃûÌû¯ºÍ²Ù×÷ÈÕÆÚºÍʱ¼ä¡£
ÀýÈ磬ÒÔÏÂÊÇÔÚLaravelÖÐʹÓÃCarbonÀàÃûÌû¯ÈÕÆÚºÍʱ¼äµÄʾÀý£º
$date = Carbon::now('Asia/Shanghai')->format('Y-m-d'); //ʹÓÃÖйú±ê׼ʱ¼äÃûÌû¯ÈÕÆÚ $time = Carbon::now('Asia/Shanghai')->format('h:i:s A'); //ʹÓÃÖйú±ê׼ʱ¼äÃûÌû¯Ê±¼ä
µÇ¼ºó¸´ÖÆ
ÈÕÆÚºÍʱ¼äµÄÍâµØ»¯
ÔÚLaravelÖУ¬Äú¿ÉÒÔÇáËɵØÍâµØ»¯ÈÕÆÚºÍʱ¼ä¡£ÍâµØ»¯ÈÕÆÚºÍʱ¼äÊǽ«ËüÃÇת»»ÎªÌض¨ÓïÑÔÇéÐεÄÀú³Ì¡£
LaravelʹÓá°i18n¡±£¨¹ú¼Ê»¯£©×é¼þÀ´ÌṩÍâµØ»¯Ïà¹Ø¹¦Ð§¡£Ä¬ÈÏÇéÐÎÏ£¬LaravelÖ§³ÖÓ¢Óï¡¢Î÷°àÑÀÓïºÍ·¨Óï¡£Äú¿ÉÒÔƾ֤ÐèÒªÌí¼ÓÆäËûÓïÑÔÖ§³Ö¡£
ÔÚÓ¦ÓóÌÐòÖУ¬Äú¿ÉÒÔÇáËÉÍâµØ»¯ÈÕÆÚºÍʱ¼ä¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º
$date = Carbon::now('Asia/Shanghai')->locale('en')->isoFormat('LLLL'); //½«ÈÕÆÚºÍʱ¼äÍâµØ»¯ÎªÓ¢Óï
µÇ¼ºó¸´ÖÆ
Õ⽫°ÑÄ¿½ñµÄÈÕÆÚºÍʱ¼äÍâµØ»¯ÎªÓ¢Óï¡£
¸ß¼¶Ê±¼ä²Ù×÷
LaravelÌṩÁËһЩ¸ß¼¶Ê±¼ä²Ù×÷£¬ÈÃʱ¼äÖÎÀíÔ½·¢ÇáËÉ¡£ÒÔÏÂÊÇһЩ֧³Ö¸ß¼¶Ê±¼ä²Ù×÷µÄʾÀý£º
£¨1£©Ìí¼ÓºÍ¼õȥʱ¼ä
Äú¿ÉÒÔʹÓÃCarbonÀàµÄaddHours()¡¢subDays()µÈÒªÁì¼òÆÓµØÌí¼Ó»ò¼õȥʱ¼ä¡£ÀýÈ磺
$date = Carbon::now()->addDays(7); //Ìí¼Ó7Ìì $date = Carbon::now()->subWeeks(2); //¼õÈ¥2ÖÜ
µÇ¼ºó¸´ÖÆ
£¨2£©ÅÌËãÈÕÆÚ¾àÀë
LaravelÌṩÁËÒ»¸öDateIntervalÀ࣬ÓÃÓÚÅÌËãÁ½¸öÈÕÆÚÖ®¼äµÄ¾àÀë¡£ÒÔÏÂÊÇÅÌËãÈÕÆÚ¾àÀëµÄʾÀý£º
$date1 = Carbon::parse('2022-12-31'); $date2 = Carbon::now(); $diff = $date1->diffInDays($date2); //ÅÌËãÁ½¸öÈÕÆÚÖ®¼äµÄÌìÊý
µÇ¼ºó¸´ÖÆ
£¨3£©ÈÕÆÚ½ÏÁ¿
Äú¿ÉÒÔʹÓÃCarbonÀàµÄ½ÏÁ¿ÒªÁìcompareTo()¡¢greaterThan()¡¢lessThan()µÈÀ´½ÏÁ¿ÈÕÆÚ¡£ÒÔÏÂÊǽÏÁ¿ÈÕÆÚµÄʾÀý£º
$date1 = Carbon::parse('2022-12-31'); $date2 = Carbon::now(); if ($date1->greaterThan($date2)) { echo 'Date1 is greater'; } else { echo 'Date2 is greater'; }
µÇ¼ºó¸´ÖÆ
×ܽá
LaravelÌṩÁËÒ»×éºÜÊÇÇ¿Ê¢µÄʱ¼ä´¦Àí¹¦Ð§£¬Ê¹Ê±¼äÖÎÀí±äµÃÔ½·¢ÇáËÉ¡£Äú¿ÉÒÔƾ֤ÐèÒªÉèÖÃÈÕÆÚºÍʱ¼äÃûÌá¢ÍâµØ»¯ÈÕÆÚºÍʱ¼ä¡¢Ö´Ðи߼¶Ê±¼ä²Ù×÷µÈ¡£ÔÚÈκÎWebÓ¦ÓóÌÐòÖУ¬Ê±¼ä´¦Àí¹ØÓÚ׼ȷµÄ¹¦Ð§ÖÁ¹ØÖ÷Òª£¬Òò´ËʹÓÃLaravelµÄʱ¼ä´¦Àí¹¦Ð§¿ÉÒÔÉÁ¿ª·¢Ô½·¢¸ßЧºÍ׼ȷ¡£
ÒÔÉϾÍÊÇÔõÑùÔÚLaravelÖÐÉèÖÃʱ¼äµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡