


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视频教程》

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
