PHP uses datetime processor Carbon instance method
Carbon date and time processing library can process time very conveniently. The github address is https://github.com/briannesbitt/carbon. This article mainly introduces the detailed explanation of PHP’s use of date and time processor Carbon to display time in a humanized manner. It has certain For reference value, those who are interested can learn about it. I hope it can help everyone.
Carbon can be easily installed through Composer
# composer require nesbot/carbon
The method of use is also very simple
<?php require 'vendor/autoload.php'; use Carbon\Carbon; //显示中文 Carbon::setLocale('zh'); //获取昨天的时间戳 $ts = Carbon::yesterday()->timestamp; //人性化显示时间 echo Carbon::createFromTimestamp($ts)->diffForHumans();
The above print result was 1 day ago
How to use it in the Laravel framework
First of all, in order to display Chinese, add it in app/Providers/AppServiceProvider.php
\Carbon\Carbon::setLocale('zh');
To the boot()
method, as follows:
public function boot(){ \Carbon\Carbon::setLocale('zh'); }
Then you can use it. For example, in a method in ArticleController, display the publication date of the article in a humanized way. If the publication date is a timestamp, quote Carbon in the header and add the following code
use Carbon\Carbon;
Humanized publishing time
Carbon::createFromTimestamp($published_at)->diffForHumans();
In addition to humanized display time, Carbon also has many time processing functions. Please refer to the official documentation for specific usage methods.
Related recommendations:
Seven very useful Carbon methods in Laravel
Share dates in PHP Time processing tool (Carbon) example
How does Carbon determine what day of the week today is?
The above is the detailed content of PHP uses datetime processor Carbon instance method. For more information, please follow other related articles on the PHP Chinese website!

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



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,

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PS card is "Loading"? Solutions include: checking the computer configuration (memory, hard disk, processor), cleaning hard disk fragmentation, updating the graphics card driver, adjusting PS settings, reinstalling PS, and developing good programming habits.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

Strict types in PHP are enabled by adding declare(strict_types=1); at the top of the file. 1) It forces type checking of function parameters and return values to prevent implicit type conversion. 2) Using strict types can improve the reliability and predictability of the code, reduce bugs, and improve maintainability and readability.

C# multi-threaded programming is a technology that allows programs to perform multiple tasks simultaneously. It can improve program efficiency by improving performance, improving responsiveness and implementing parallel processing. While the Thread class provides a way to create threads directly, advanced tools such as Task and async/await can provide safer asynchronous operations and a cleaner code structure. Common challenges in multithreaded programming include deadlocks, race conditions, and resource leakage, which require careful design of threading models and the use of appropriate synchronization mechanisms to avoid these problems.

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.
