


PHP date and time processing extension package worth knowing: Carbon
本篇文章给大家推荐一个扩展包:Carbon,PHP date and time processing extension package worth knowing: Carbon 中日期 / 时间处理,你只需要这个扩展包就够了!
在 PHP date and time processing extension package worth knowing: Carbon 中使用日期和时间并不是容易或清晰的任务。我们必须处理 strtotime
,格式化问题,大量计算等等。
这个漂亮的包叫做 Carbon 可以帮助在 PHP date and time processing extension package worth knowing: Carbon 开发中处理日期/时间变得更加简单、更语义化,从而使得我们的代码更容易阅读和维护。
Carbon
Carbon 是由 Brian Nesbit 开发的一个包,它扩展了 PHP date and time processing extension package worth knowing: Carbon 自己的 DateTime 类。
它提供了一些很好的功能来处理 PHP date and time processing extension package worth knowing: Carbon 中的日期,特别是诸如:
- 处理时区
- 轻松获取当前时间
- 将 datetime 转换成可读的内容
- 将英语短语解析成 datetime (first day of January 2016)
- 日期的加减 (+ 2 weeks, -6 months)
- 处理日期的语义方法
所有的这些都带来了一个非常有用的包,使得这些在 PHP date and time processing extension package worth knowing: Carbon 中处理时间非常容易。
设置
为了使用 Carbon ,你需要从 Carbon
命名空间中导入 Carbon 。幸运的是,在 Laravel 中已经包括了 Carbon ,所以不需要和 Composer 一起添加。
当我们需要使用 Carbon 的时候,我们可以这样导入它:
<?php use Carbon\Carbon;
在导入之后,让我们看看我们可以用这个很棒的包做一些很酷的事情。
获取特定的日期/时间
// 获取当前时间 - 2015-12-19 10:10:54 $current = Carbon::now(); $current = new Carbon(); // 获取今天 - 2015-12-19 00:00:00 $today = Carbon::today(); // 获取昨天 - 2015-12-18 00:00:00 $yesterday = Carbon::yesterday(); // 获取明天 - 2015-12-20 00:00:00 $tomorrow = Carbon::tomorrow(); // 解析特定字符串 - 2016-01-01 00:00:00 $newYear = new Carbon('first day of January 2016'); // 设定一个特定的时区 - 2016-01-01 00:00:00 $newYearPST = new Carbon('first day of January 2016', 'America\Pacific');
除了快速定义日期/时间方法之外,Carbon 也可以让我们从特定数量的参数中创建时间。
Carbon::createFromDate($year, $month, $day, $tz); Carbon::createFromTime($hour, $minute, $second, $tz); Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);
当你以一种通常不被 Carbon 识别的格式获得某种日期或时间时,这些是非常有用的。如果你为任何一个参数传递 null
值,则它默认会使用当前日期/时间传递 。
操作日期/时间
抓取日期/时间并不是你在处理日期时唯一要做的事情。你经常需要操作日期或时间。
例如,当为一个用户创建一个试用期时,你将希望试用期在一定时间后过期。假设我们有 30 天的试用期。我们可以用 add
和 subtract
很容易的计算出时间。
在这段试用期内,我们会:
// 获取当前时间 $current = Carbon::now(); // 添加 30 天到当前时间 $trialExpires = $current->addDays(30);
从 Carbon 文档 中,我们可以找到一些其他的 add()
和 sub()
方法:
$dt = Carbon::create(2012, 1, 31, 0); echo $dt->toDateTimeString(); // 2012-01-31 00:00:00 echo $dt->addYears(5); // 2017-01-31 00:00:00 echo $dt->addYear(); // 2018-01-31 00:00:00 echo $dt->subYear(); // 2017-01-31 00:00:00 echo $dt->subYears(5); // 2012-01-31 00:00:00 echo $dt->addMonths(60); // 2017-01-31 00:00:00 echo $dt->addMonth(); // 2017-03-03 00:00:00 equivalent of $dt->month($dt->month + 1); so it wraps echo $dt->subMonth(); // 2017-02-03 00:00:00 echo $dt->subMonths(60); // 2012-02-03 00:00:00 echo $dt->addDays(29); // 2012-03-03 00:00:00 echo $dt->addDay(); // 2012-03-04 00:00:00 echo $dt->subDay(); // 2012-03-03 00:00:00 echo $dt->subDays(29); // 2012-02-03 00:00:00 echo $dt->addWeekdays(4); // 2012-02-09 00:00:00 echo $dt->addWeekday(); // 2012-02-10 00:00:00 echo $dt->subWeekday(); // 2012-02-09 00:00:00 echo $dt->subWeekdays(4); // 2012-02-03 00:00:00 echo $dt->addWeeks(3); // 2012-02-24 00:00:00 echo $dt->addWeek(); // 2012-03-02 00:00:00 echo $dt->subWeek(); // 2012-02-24 00:00:00 echo $dt->subWeeks(3); // 2012-02-03 00:00:00 echo $dt->addHours(24); // 2012-02-04 00:00:00 echo $dt->addHour(); // 2012-02-04 01:00:00 echo $dt->subHour(); // 2012-02-04 00:00:00 echo $dt->subHours(24); // 2012-02-03 00:00:00 echo $dt->addMinutes(61); // 2012-02-03 01:01:00 echo $dt->addMinute(); // 2012-02-03 01:02:00 echo $dt->subMinute(); // 2012-02-03 01:01:00 echo $dt->subMinutes(61); // 2012-02-03 00:00:00 echo $dt->addSeconds(61); // 2012-02-03 00:01:01 echo $dt->addSecond(); // 2012-02-03 00:01:02 echo $dt->subSecond(); // 2012-02-03 00:01:01 echo $dt->subSeconds(61); // 2012-02-03 00:00:00
Getters and Setters
另外一种快速操作或读取时间的方法是使用可用的 getters 和 setters 。
$dt = Carbon::now(); // 设置一些参数 $dt->year = 2015; $dt->month = 04; $dt->day = 21; $dt->hour = 22; $dt->minute = 32; $dt->second = 5; // 获取一些参数 var_dump($dt->year); var_dump($dt->month); var_dump($dt->day); var_dump($dt->hour); var_dump($dt->second); var_dump($dt->dayOfWeek); var_dump($dt->dayOfYear); var_dump($dt->weekOfMonth); var_dump($dt->daysInMonth);
我们甚至还可以把一些 setter 串在一起。
$dt = Carbon::now(); $dt->year(1975)->month(5)->day(21)->hour(22)->minute(32)->second(5)->toDateTimeString(); $dt->setDate(1975, 5, 21)->setTime(22, 32, 5)->toDateTimeString(); $dt->setDateTime(1975, 5, 21, 22, 32, 5)->toDateTimeString();
格式化
在上面的示例中,你可能注意到了 ->toDateTimeString()
方法。我们可以方便的为达到我们的目的去进行格式化。在这种情况下,我们得到了一个日期时间字符串。
$dt = Carbon::now(); echo $dt->toDateString(); // 2015-12-19 echo $dt->toFormattedDateString(); // Dec 19, 2015 echo $dt->toTimeString(); // 10:10:16 echo $dt->toDateTimeString(); // 2015-12-19 10:10:16 echo $dt->toDayDateTimeString(); // Sat, Dec 19, 2015 10:10 AM // ……当然 format() 也可以这样用 echo $dt->format('l jS \\of F Y h:i:s A'); // Saturday 19th of December 2015 10:10:16 AM
相对时间
通过 diff()
方法可以很容易的显示相对时间。
例如,我们有一篇博客,并且我们想显示它是在 三小时 前发布的。可以利用这些方法。
求时间差
这些方法用于求两个时间的时间差。
$current = Carbon::now(); $dt = Carbon::now(); $dt = $dt->subHours(6); echo $dt->diffInHours($current, false); // 6,相当于 $current-$dt,这里的false表示结果不取绝对值,默认是值是true echo $current->diffInHours($dt, false); // -6,相当于 $dt-$current $future = $current->addMonth(); $past = $current->subMonths(2); echo $current->diffInDays($future); // 31 echo $current->diffInDays($past); // -62
显示人类容易阅读的时间差
在过去的几年,显示相对时间变得越来越流行。在 Twitter 和 Facebook 等社交网络中经常可以看到。
例如,将时间显示为 3 小时前 比显示 上午 8:12,更适合人类阅读。
这些方法被用于计算时间差,并转换为人类可阅读的格式。
这里有四种表达时间差的方式:
- 将一个过去的时间和现在做比较:
- 1 小时前
- 5 个月前
- 将一个未来的时间和现在做比较:
- 1 小时后
- 5 个月后
- 将一个过去的时间和另一个时间做比较:
- 1 小时前
- 5 小时前
- 将一个未来的时间和另一个做比较:
- 1 小时后
- 5 小时后
$dt = Carbon::now(); $past = $dt->subMonth(); $future = $dt->addMonth(); echo $dt->subDays(10)->diffForHumans(); // 10 天前 echo $dt->diffForHumans($past); // 1 个月前 echo $dt->diffForHumans($future); // 1 个月前
总结
Carbon 能做的远远不止这些。请务必查看 Carbon 官方文档。希望这能帮助你在 PHP date and time processing extension package worth knowing: Carbon 中更容易的使用日期 / 时间并加快开发效率!
推荐学习:《PHP date and time processing extension package worth knowing: Carbon视频教程》

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools