当前位置:网站首页>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
边栏推荐
- C # customize and dynamically switch cursor
- Solve idea:class' xxx 'not found in module' xxx‘
- KS009基于SSH实现宠物管理系统
- Visual studio 2019 shortcut notes
- Matlab farthest point sampling (FPS improved version)
- One of the basics - overview of sta Basics
- 06. on several ways of redis persistence
- OCR的一些项目
- Why build a personal blog
- 短视频平台开发,依靠DrawerLayout实现侧滑菜单效果
猜你喜欢

Introduction and principle analysis of cluster and LVS

Digital IC design process summary

基础知识之一——STA基础概述

What will Web3 bring in the future?

Neo4j installation, operation, project construction and function realization

45 year old programmer tells you: why do programmers want to change jobs? It's too true

Sun Yuchen told Swiss media Bilan that the bear market will not last long

Log logrus third party library usage

基础知识之三——标准单元库

neo4j安装、运行以及项目的构建和功能实现
随机推荐
【队列】933. Number of Recent Calls
元宇宙为 VR/AR 带来的新机会
Strictmode analysis activity leakage -strictmode principle (3)
编译安装oh-my-zsh
dc_labs--lab1的学习与总结
[Deepin] 常用集合
None of the following candidates is applicable because of a receiver type mismatch
Chromatic judgement bipartite graph
Understanding and application of Qt5 layout in creation
Use strictmode strictmode principle (1)
sort自定义函数
Koa koa combine routes sub route management
[stack] 921 Minimum Add to Make Parentheses Valid
短视频平台开发,依靠DrawerLayout实现侧滑菜单效果
1175. Prime Arrangements
直播商城源码,实现左右联动商品分类页面
做生意更加务实
Unknown database connection database error
New opportunities for vr/ar brought by metauniverse
06. on several ways of redis persistence