当前位置:网站首页>Use of laravel carbon time processing class
Use of laravel carbon time processing class
2022-07-01 01:35:00 【Know the black and accept the white】
Read the directory
1、 Get the current time
// You can pass a valid time zone as a parameter
echo Carbon::now();//2019-08-17 09:27:49
echo Carbon::now('America/Los_Angeles');//2019-08-17 02:27:49
2、 Get yesterday, today and tomorrow
// You can pass a valid time zone as a parameter
echo Carbon::today();//2019-08-17 00:00:00
echo Carbon::tomorrow('America/Los_Angeles');//2019-08-18 00:00:00
echo Carbon::yesterday();//2019-08-16 00:00:00
3、 Convert string type
echo Carbon::today()->toDateTimeString();
echo Carbon::yesterday()->toDateTimeString();
echo Carbon::tomorrow()->toDateTimeString();
4、 Date resolution
echo Carbon::parse('2016-10-15')->toDateTimeString(); //2016-10-15 00:00:00
echo Carbon::parse('2016-10-15')->toDateTimeString(); //2016-10-15 00:00:00
echo Carbon::parse('2016-10-15 00:10:25')->toDateTimeString(); //2016-10-15 00:10:25
echo Carbon::parse('today')->toDateTimeString(); //2016-10-15 00:00:00
echo Carbon::parse('yesterday')->toDateTimeString(); //2016-10-14 00:00:00
echo Carbon::parse('tomorrow')->toDateTimeString(); //2016-10-16 00:00:00
echo Carbon::parse('2 days ago')->toDateTimeString(); //2016-10-13 20:49:53
echo Carbon::parse('+3 days')->toDateTimeString(); //2016-10-18 20:49:53
echo Carbon::parse('+2 weeks')->toDateTimeString(); //2016-10-29 20:49:53
echo Carbon::parse('+4 months')->toDateTimeString(); //2017-02-15 20:49:53
echo Carbon::parse('-1 year')->toDateTimeString(); //2015-10-15 20:49:53
echo Carbon::parse('next wednesday')->toDateTimeString(); //2016-10-19 00:00:00
echo Carbon::parse('last friday')->toDateTimeString(); //2016-10-14 00:00:00
5、 Construction date
$year = '2015';
$month = '04';
$day = '12';
echo Carbon::createFromDate($year, $month, $day); //2015-04-12 20:55:59
$hour = '02';
$minute = '15':
$second = '30';
echo Carbon::create($year, $month, $day, $hour, $minute, $second); //2015-04-12 02:15:30
echo Carbon::createFromDate(null, 12, 25); // Year defaults to the current year
6、 Date operations
//add- Add sub- reduce
echo Carbon::now()->addDays(25); //2016-11-09 14:00:01
echo Carbon::now()->addWeeks(3); //2016-11-05 14:00:01
echo Carbon::now()->addHours(25); //2016-10-16 15:00:01
echo Carbon::now()->subHours(2); //2016-10-15 12:00:01
echo Carbon::now()->addHours(2)->addMinutes(12); //2016-10-15 16:12:01
echo Carbon::now()->modify('+15 days'); //2016-10-30 14:00:01
echo Carbon::now()->modify('-2 days'); //2016-10-13 14:00:01
7、 Date comparison
/* min – Return the minimum date . max – Returns the maximum date . eq – Judge whether two dates are equal . gt – Determine whether the first date is greater than the second date . lt – Judge whether the first date is smaller than the second date . gte – Judge whether the first date is greater than or equal to the second date . lte – Judge whether the first date is less than or equal to the second date . */
echo Carbon::now()->tzName; // America/Toronto
$first = Carbon::create(2012, 9, 5, 23, 26, 11);
$second = Carbon::create(2012, 9, 5, 20, 26, 11, 'America/Vancouver');
echo $first->toDateTimeString(); // 2012-09-05 23:26:11
echo $first->tzName; // America/Toronto
echo $second->toDateTimeString(); // 2012-09-05 20:26:11
echo $second->tzName; // America/Vancouver
var_dump($first->eq($second)); // bool(true)
var_dump($first->ne($second)); // bool(false)
var_dump($first->gt($second)); // bool(false)
var_dump($first->gte($second)); // bool(true)
var_dump($first->lt($second)); // bool(false)
var_dump($first->lte($second)); // bool(true)
$first->setDateTime(2012, 1, 1, 0, 0, 0);
$second->setDateTime(2012, 1, 1, 0, 0, 0); // Remember tz is 'America/Vancouver'
var_dump($first->eq($second)); // bool(false)
var_dump($first->ne($second)); // bool(true)
var_dump($first->gt($second)); // bool(false)
var_dump($first->gte($second)); // bool(false)
var_dump($first->lt($second)); // bool(true)
var_dump($first->lte($second)); // bool(true)
8、 Determine whether a date is between two dates
$first = Carbon::create(2012, 9, 5, 1);
$second = Carbon::create(2012, 9, 5, 5);
var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second)); // bool(true)
var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second)); // bool(true)
var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second, false)); // bool(false)
9、 Auxiliary method
$dt = Carbon::now();
$dt->isWeekday();
$dt->isWeekend();
$dt->isYesterday();
$dt->isToday();
$dt->isTomorrow();
$dt->isFuture();
$dt->isPast();
$dt->isLeapYear();
$dt->isSameDay(Carbon::now());
$born = Carbon::createFromDate(1987, 4, 23);
$noCake = Carbon::createFromDate(2014, 9, 26);
$yesCake = Carbon::createFromDate(2014, 4, 23);
$overTheHill = Carbon::now()->subYears(50);
var_dump($born->isBirthday($noCake)); // bool(false)
var_dump($born->isBirthday($yesCake)); // bool(true)
var_dump($overTheHill->isBirthday()); // bool(true) -> default compare it to today!
10、 Turn time into a text description
echo Carbon::now()->subDays(5)->diffForHumans(); // 5 Days ago,
echo Carbon::now()->diffForHumans(Carbon::now()->subYear()); // 1 After year
$dt = Carbon::createFromDate(2011, 8, 1);
echo $dt->diffForHumans($dt->copy()->addMonth()); // 1 Month ago,
echo $dt->diffForHumans($dt->copy()->subMonth()); // 11 After month
echo Carbon::now()->addSeconds(5)->diffForHumans(); // 5 Seconds from now
echo Carbon::now()->subDays(24)->diffForHumans(); // 3 Zhou Qian
// You can set the second parameter to true To delete ' front '、' From now on ' Other modifiers
echo Carbon::now()->subDays(24)->diffForHumans(null, true); // 3 Zhou
边栏推荐
- Zero of DC learning notes -- overview and basic process introduction
- The argument type 'function' can't be assigned to the parameter type 'void function()‘
- Why build a personal blog
- 农产品换房?“变相”购房补贴!
- 【模拟】922. Sort Array By Parity II
- Open3d point cloud color rendering
- neo4j安装、运行以及项目的构建和功能实现
- [stack] 921 Minimum Add to Make Parentheses Valid
- 图的连通性基础
- Applet Custom Grid
猜你喜欢
短信在企业中的应用有哪些?
Creating ASCII art with C #
neo4j安装、运行以及项目的构建和功能实现
[problem handled] -nvidia SMI command cannot obtain the GPU process number of its own container and the external GPU process number
Service grid ASM year end summary: how do end users use the service grid?
【Qt5-基础篇】随机数显示屏展示
The liquor and tourism sector recovers, and Yaduo continues to dream of listing. How far is it from "the first share of the new accommodation economy"?
Understanding and application of Qt5 layout in creation
Institute of Microbiology, commonly used biochemical reactions in microbiological testing
Basic knowledge II - Basic definitions related to sta
随机推荐
TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to
C# 自定义并动态切换光标
基础知识之二——STA相关的基本定义
gin_gorm
生意和投资的思考
3dsmax plug-in development traversal node object and object acquisition and inode transformation matrix description
DC學習筆記正式篇之零——綜述與基本流程介紹
OCR的一些项目
What are the functions of soil microorganisms in microbial detection?
Uniapp official component clicking item is invalid, solution
微生物健康,食品微生物检测为什么很重要
What will Web3 bring in the future?
Sécurité et santé microbiennes, qu'est - ce que le traitement biologique?
【qt5-tab标签精讲】Tab标签及内容分层解析
【动态规划】路径dp:931. Minimum Falling Path Sum
Handsontable數據網格組件
Exploration and practice of "flow batch integration" in JD
Complete software development process
Relationship between ASCII, Unicode, GBK, UTF-8
Open3d point cloud bounding box