Home php教程 php手册 php计算时间间隔实现程序代码

php计算时间间隔实现程序代码

May 25, 2016 pm 04:56 PM
explode

在文章里面我说了很多关于php计算时间间隔实现程序代码的实现方法原理以及分析如何来做这个功能,有需用的同学可以仔细看看哦。

下面实例是告诉我们在论坛有看到关于多少秒钟前发了帖子等这些功能,

分析

实际时间 PHP

1秒 2“秒”

______________________

30秒==60

5分钟==60*10 //这里我觉得应该是五分钟,而不是一分钟,个人觉得 一分钟的话应该是 60*2

10分钟==60*20

30分钟==60*60

1小时==60*60*2

2.5小时==60*60*5


原理

用当前时间time(),减去你信息录入时的新增时间,(注意是时间戳),然后得出的差数,就是秒数,再根据自己的需要来转换啊。例如你想转换成分钟,就除以60。

实例

 代码如下 复制代码

/**
 * 时间差计算
 *
 * @param Timestamp $time
 * @return String Time Elapsed
 * @author Shelley Shyan
 * @copyright http://www.phprm.com (Professional PHP Architecture)
 */
function time2Units ($time)
{
   $year   = floor($time / 60 / 60 / 24 / 365);
   $time  -= $year * 60 * 60 * 24 * 365;
   $month  = floor($time / 60 / 60 / 24 / 30);
   $time  -= $month * 60 * 60 * 24 * 30;
   $week   = floor($time / 60 / 60 / 24 / 7);
   $time  -= $week * 60 * 60 * 24 * 7;
   $day    = floor($time / 60 / 60 / 24);
   $time  -= $day * 60 * 60 * 24;
   $hour   = floor($time / 60 / 60);
   $time  -= $hour * 60 * 60;
   $minute = floor($time / 60);
   $time  -= $minute * 60;
   $second = $time;
   $elapse = '';

   $unitArr = array('年'  =>'year', '个月'=>'month',  '周'=>'week', '天'=>'day',
                    '小时'=>'hour', '分钟'=>'minute', '秒'=>'second'
                    );

   foreach ( $unitArr as $cn => $u )
   {
       if ( $$u > 0 )
       {
           $elapse = $$u . $cn;
           break;
       }
   }

   return $elapse;
}

$past = 2052345678; // Some timestamp in the past
$now  = time();     // Current timestamp
$diff = $now - $past;

echo '发表于' . time2Units($diff) . '前';
?>

这是一个刚入门的同学写的

 代码如下 复制代码

    $regist1 = "05/12/2006";
    $regist2 = "10/05/2007";

    list($month1,$day1,$year1) = explode("/",$regist1);
    list($month2,$day2,$year2) = explode("/",$regist2);

    $regist1 = mktime(0,0,0,$month1,$day1,$year1);
    $regist2 = mktime(0,0,0,$month2,$day2,$year2);

    $time_difference = $regist2-$regist1;

    echo ("时间差:");
    echo date("Y",$time_difference) - 1970;
    echo ("年");
    echo date("m",$time_difference) - 1;
    echo ("个月");
    echo date("d",$time_difference) - 1;
    echo ("天");
?>


好了喜欢就自己选择用吧。



本文地址:

转载随意,但请附上文章地址:-)

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP explode function and solve errors How to use PHP explode function and solve errors Mar 10, 2024 am 09:18 AM

The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

Common errors and solutions when using the explode function in PHP Common errors and solutions when using the explode function in PHP Mar 11, 2024 am 08:33 AM

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

Split and merge strings using the explode and implode functions Split and merge strings using the explode and implode functions Jun 15, 2023 pm 08:42 PM

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra

Split string into array using PHP function 'explode' Split string into array using PHP function 'explode' Jul 24, 2023 pm 11:09 PM

Use the PHP function "explode" to split a string into an array. In PHP development, you often encounter situations where you need to split a string according to the specified delimiter. At this time, we can use PHP's built-in function "explode" to convert string to array. This article will introduce how to use the "explode" function to split a string and give relevant code examples. The basic syntax of the "explode" function is as follows: arrayexplode(

What should I do if php explode reports an error? What should I do if php explode reports an error? Jan 18, 2023 am 10:13 AM

Solution to php explode error: 1. Find and open the erroneous PHP code; 2. Find the explode function part; 3. Modify the code to "dump(explode(',',$str));die;", that is, use Just comma-separate the array.

How to use explode function to split string in PHP How to use explode function to split string in PHP Jun 26, 2023 pm 12:03 PM

In the PHP language, there are many basic functions that can help us process strings quickly and efficiently. Among them, the explode function is a very practical string splitting function. It can split a string into arrays according to the specified delimiter, and then perform more flexible string operations. In this article, we will introduce how to use the explode function to split strings in PHP. 1. The format of the explode function. The format of the explode function in the PHP language is as follows: explode(separa

Use PHP function 'explode' to split a string into multiple substrings Use PHP function 'explode' to split a string into multiple substrings Jul 25, 2023 pm 06:29 PM

Use the PHP function "explode" to split a string into multiple substrings. In PHP programming, we often encounter situations where we need to split a string into multiple substrings. At this time, PHP provides a very convenient function "explode" that can help us easily implement this function. The syntax of the "explode" function is as follows: arrayexplode(string$delimiter,string$string[,in

What is the explode() function in PHP? What is the explode() function in PHP? Sep 04, 2023 pm 01:01 PM

In this article, learn how to use the PHPExplode() function, which is a predefined built-in PHP function. The explode function is used to "Split a string into smaller contents. The explode function in PHP enables us to break a string into smaller contents by breaking. This delimiter is called a delimiter. Syntax explode(delimiter, string, Number of elements) Parameter explosion function accepts three parameters, two of which are mandatory and one is optional. Let us discuss these three parameters. Separator This character specifies the critical point or point at which the string will be split, i.e. Whenever this character is found in a string, it symbolizes the end of one element and the beginning of another element in the array. String input string

See all articles