php method to convert components in seconds: 1. Create a PHP sample file; 2. Create "class Calendar{...}"; 3. Use the "function formatSecond(){...}" method Just implement the conversion.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
How to convert ingredients in php seconds?
php automatically converts seconds into components, hours, days, weeks, months, years...
<?php class Calendar { /** * 一分钟的秒数 */ const TIME_MINUTE = 60; /** * 一小时的秒数 */ const TIME_HOURS = 3600; /** * 一天的秒数 */ const TIME_DAY = 3600 * 24; /** * 一周的秒数 */ const TIME_WEEK = 3600 * 24 * 7; /** * 一月的秒数 */ const TIME_MONTH = 3600 * 24 * 30; /** * 一年(平年)的秒数 */ const TIME_YEAR = 3600 * 24 * 365; /** * 一年(闰年年)的秒数 */ const TIME_YEAR_INTERCALARY = 3600 * 24 * 366; /** * 时间=》对应名称 */ const TIME_NAMES = [ 0 => '秒', self::TIME_MINUTE => '分钟', self::TIME_HOURS => '小时', self::TIME_DAY => '天', self::TIME_WEEK => '周', self::TIME_MONTH => '月', self::TIME_YEAR => '年', ]; /** * 格式化秒自动到 分或者时 或者 天 等.... * @param int $second * @param string[] $unitString 可以自己定义 * @return string */ public static function formatSecond($second = 0, $unitString = self::TIME_NAMES) { ksort($unitString); if ($second < self::TIME_MINUTE) return $second . $unitString[0]; krsort($unitString); foreach ($unitString as $time => $unitName) { if ($time <= 0) continue; $result = floor($second / $time); if ($result >= 1) return $result . $unitName; } return null; } }
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert components in seconds in php. 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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download
The most popular open source editor

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