PHP returns the difference between two points in time and displays it in a very humanized way. Humanized design. Translation. Humanized service. English. Humanized city.

WBOY
Release: 2016-07-29 08:55:15
Original
1203 people have browsed it

1. Custom function (super smart)

//Returns the difference between two time points and displays it in a very user-friendly way

//The function has two parameters. The first parameter refers to the start time, the default value is 1; the second parameter is the current (or end) time, the default is time()
<span style="font-size:18px;">function timespan($seconds = 1, $time = '')
	{

		if ( ! is_numeric($seconds))
		{
			$seconds = 1;
		}

		if ( ! is_numeric($time))
		{
			$time = time();
		}

		if ($time <= $seconds)
		{
			$seconds = 1;
		}
		else
		{
			$seconds = $time - $seconds;
		}

		$str = &#39;&#39;;
		$years = floor($seconds / 31536000);

		if ($years > 0)
		{
			$str .= $years.' 年, ';
		}

		$seconds -= $years * 31536000;
		$months = floor($seconds / 2628000);

		if ($years > 0 OR $months > 0)
		{
			if ($months > 0)
			{
				$str .= $months.' 月, ';
			}

			$seconds -= $months * 2628000;
		}

		$weeks = floor($seconds / 604800);

		if ($years > 0 OR $months > 0 OR $weeks > 0)
		{
			if ($weeks > 0)
			{
				$str .= $weeks.' 周, ';
			}

			$seconds -= $weeks * 604800;
		}

		$days = floor($seconds / 86400);

		if ($months > 0 OR $weeks > 0 OR $days > 0)
		{
			if ($days > 0)
			{
				$str .= $days.' 天, ';
			}

			$seconds -= $days * 86400;
		}

		$hours = floor($seconds / 3600);

		if ($days > 0 OR $hours > 0)
		{
			if ($hours > 0)
			{
				$str .= $hours.' 小时, ';
			}

			$seconds -= $hours * 3600;
		}

		$minutes = floor($seconds / 60);

		if ($days > 0 OR $hours > 0 OR $minutes > 0)
		{
			if ($minutes > 0)
			{
				$str .= $minutes.' 分钟, ';
			}

			$seconds -= $minutes * 60;
		}

		if ($str == '')
		{
			$str .= $seconds.' 秒, ';
		}

		return substr(trim($str), 0, -1);
    	}</span>
Copy after login

2. It is used in the php file as follows:

人性化的 英文,人性化的 英语,人性化的服务,人性化的翻译,人性化的设计,人性化的管理,人性化的设计 翻译,人性化的服务 英文,人性化的城市

is used in the template of the tp framework as follows:

人性化的 英文,人性化的 英语,人性化的服务,人性化的翻译,人性化的设计,人性化的管理,人性化的设计 翻译,人性化的服务 英文,人性化的城市

The above introduces the difference between the two time points returned by PHP, and displays it in a very user-friendly way, including human-friendly aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!