PHP calculates a few ago, hours ago, days ago several functions, class sharing _php tutorial

Source: Internet
Author: User
First, the function realization
Example 1:
Copy CodeThe code is as follows:
function Time_tran ($the _time) {
$now _time = Date ("Y-m-d h:i:s", Time () +8*60*60);
$now _time = strtotime ($now _time);
$show _time = strtotime ($the _time);
$dur = $now _time-$show _time;
if ($dur < 0) {
return $the _time;
}else{
if ($dur < 60) {
return $dur. ' seconds ago ';
}else{
if ($dur < 3600) {
Return floor ($dur/60). ' Minutes ago ';
}else{
if ($dur < 86400) {
Return floor ($dur/3600). ' Hours ago ';
}else{
if ($dur < 259200) {//within 3 days
Return floor ($dur/86400). ' Days ago ';
}else{
return $the _time;
}
}
}

Example 2:
Copy CodeThe code is as follows: function Format_date ($time) {
$t =time ()-$time;
$f =array (
' 31536000 ' = ' year ',
' 2592000 ' + ' months ',
' 604800 ' = ' Week ',
' 86400 ' = ' Day ',
' 3600 ' = ' hours ',
' Five ' and ' minutes ',
' 1 ' = ' s '
);
foreach ($f as $k = = $v) {
if (0! = $c =floor ($t/(int) $k)) {
Return $c. $v. ' Ago ';
}
}
}
?>

Instance 3:
copy code code is as follows:
function Formattime ($date) {
$str = ';
$timer = Strtotime ($date);
$diff = $_server[' request_time ')-$timer;
$day = Floor ($diff/86400);
$free = $diff% 86400;
if ($day > 0) {
return $day. " Days ago ";
}else{
if ($free >0) {
$hour = floor ($free/3600);
$free = $free% 3600;
if ($hour >0) {
return $hour. " Hours ago ";
}else{
if ($free >0) {
$min = floor ($free/60);
$free = $free% 60;
if ($min >0) {
return $min. " Minutes ago ";
}else{
if ($free >0) {
return $free. " Seconds ago ";
}else{
return ' just ';
}
}
}else{
return ' just ';
}
}
}else{
return ' just ';
}
}
}

Example 4:
Copy the Code code as follows:
function Time_tran ($the _time) {
$now _time = Date ("Y-m-d h:i:s", Time () +8*60*60);
$now _time = strtotime ($now _time);
$show _time = strtotime ($the _time);
$dur = $now _time-$show _time;
if ($dur < 0) {
return $the _time;
}else{
if ($dur < 60) {
return $dur. ' seconds ago ';
}else{
if ($dur < 3600) {
Return floor ($dur/60). ' Minutes ago ';
}else{
if ($dur < 86400) {
Return floor ($dur/3600). ' Hours ago ';
}else{
if ($dur < 259200) {//within 3 days
Return floor ($dur/86400). ' Days ago ';
}else{
return $the _time;
}
}
}
}
}
}

Second, the realization of the class
Copy the Code Code as follows:/ *
* Author:solon Ring
* time:2011-11-02
* Hair bo Time calculation (year, month, day, hour, minute, second)
* $createtime can be the current time
* $gettime The time you're going to pass in
*/

Class mygettime{

function __construct ($createtime, $gettime) {
$this->createtime = $createtime;
$this->gettime = $gettime;
}

function getseconds ()
{
return $this->createtime-$this->gettime;
}

function getminutes ()
{
Return ($this->createtime-$this->gettime)/(60);
}

function GetHours ()
{
Return ($this->createtime-$this->gettime)/(60*60);
}

function GetDay ()
{
Return ($this->createtime-$this->gettime)/(60*60*24);
}

function GetMonth ()
{
Return ($this->createtime-$this->gettime)/(60*60*24*30);
}

function GetYear ()
{
Return ($this->createtime-$this->gettime)/(60*60*24*30*12);
}

Function index ()
{
if ($this->getyear () > 1)
{
if ($this->getyear () > 2)
{
Return date ("y-m-d", $this->gettime);
Exit ();
}
Return Intval ($this->getyear ()). "Years ago";
Exit ();
}

if ($this->getmonth () > 1)
{
Return Intval ($this->getmonth ()). "Months ago";
Exit ();
}

if ($this->getday () > 1)
{
Return Intval ($this->getday ()). "Days Ago";
Exit ();
}

if ($this->gethours () > 1)
{
Return Intval ($this->gethours ()). "Hours ago";
Exit ();
}

if ($this->getminutes () > 1)
{
Return Intval ($this->getminutes ()). "Minutes Ago";
Exit ();
}

if ($this->getseconds () > 1)
{
Return Intval ($this->getseconds ()-1). "Seconds Ago";
Exit ();
}

}

}
Use instances of the class
/*
*
* Call class Output mode
*
* $a = new Mygettime (Time (), Strtotime (' -25 month '));
* Echo iconv (' Utf-8 ', ' gb2312 ', $a->index ())? Iconv (' Utf-8 ', ' gb2312 ', $a->index ()): Iconv (' utf-8 ', ' gb2312 ', ' current ' );
*
*/

http://www.bkjia.com/PHPjc/751508.html www.bkjia.com true http://www.bkjia.com/PHPjc/751508.html techarticle one, the function implementation Example 1: Copy code code as follows: function Time_tran ($the _time) {$now _time = date ("Y-m-d h:i:s", Time () +8*60*60); $now _time = Strt Otime ($now _time); $show _time ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.