Processing time in PHP is troublesome, especially when it comes to time format conversion, time zone conversion, time comparison and other functions. Previous articles on this siteAdjust the display of time based on the freshness of the article, it involves time processing. Interested friends can take a look at the implemented code. In order to make date and time processing simpler, the Carbon date and time processing library appeared. From then on, we can easily process time in PHP.
Carbon’s concise code and user-friendly syntax
The following is a piece of code to implement monthly membership subscription or renewal. In the first lineCarbon::now( 'Asia/Shanghai' )Initialize a time to the presentCarbon 对象,$day->addMonth()The effect is based on the current time, plus one month. Friends who are more familiar with PHP can imagine what the code would look like if PHP native functions were directly used to implement this function.
$day_now = Carbon::now( 'Asia/Shanghai' );
$day = $day_now;
// 如果是老会员,获取过期时间以便升级
if ( $old_expiration_day ) {
$day = Carbon::createFromFormat( 'Y-m-d H:i:s', $old_expiration_day );
}
$new_expiration_day = $day->addMonth()->toDateTimeString();
update_user_meta( $user_id, 'expiration_day', $new_expiration_day );
$user = new WP_User( $user_id );
$user->set_role( 'monthly' );
Date and time addition and subtraction
Initializing a Carbon object, we can directly add or subtract a day, a week, a month, a year, etc. based on this object without having to calculate how many minutes and seconds there are in a year.
$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
...
Display humanized date and time
The following code displays a humanized time by adding or subtracting time. For example, instead of displaying April 8, 2017, it displays one month later. The user looks and knows when the displayed date is without thinking.
echo Carbon::now()->subDays(5)->diffForHumans(); // 5 天前
echo Carbon::now()->diffForHumans(Carbon::now()->subYear()); // 1 年后
$dt = Carbon::createFromDate(2011, 8, 1);
echo $dt->diffForHumans($dt->copy()->addMonth()); // 1 月前
echo $dt->diffForHumans($dt->copy()->subMonth()); // 1 月后
echo Carbon::now()->addSeconds(5)->diffForHumans(); // 5 秒后
echo Carbon::now()->subDays(24)->diffForHumans(); // 3 周前
echo Carbon::now()->subDays(24)->diffForHumans(null, true); // 3 周
Date and time comparison
Need to determine whether a certain time is in the present, past, or future? Carbon also provides convenience methods directly.
$dt = Carbon::now();
$dt->isWeekday();
$dt->isWeekend();
$dt->isYesterday();
$dt->isToday();
$dt->isTomorrow();
$dt->isFuture();
$dt->isPast();
$dt->isLeapYear();
Carbon can help us process dates and times conveniently and accurately, reducing the amount of code and improving efficiency. The most popular PHP framework Laravel uses the Carbon library to handle date and time. This article only introduces some of the usage methods of Carbon. For more usage methods, please refer toPHP Carbon date and time processing library official documentation。
