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

Source: Internet
Author: User
Tags diff

One, function realization
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 = 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) {//3 days
Return floor ($dur/86400). ' Days ago ';
}else{
return $the _time;
}
}
}

Example 2:
Copy code code as follows:
<?php
function format_date ($time) {
    $t =time ()-$ Time
    $f =array (
        ' 31536000 ' => ' year ',
         ' 2592000 ' => ' month ',
        ' 604800 ' = > ' Week ',
        ' 86400 ' => ' days ',
         ' 3600 ' => ' hours ',
        ' => ' minutes ',
         ' 1 ' => ' sec '
   );
    foreach ($f as $k => $v)     {
        if (0!= $c =floor ($t/(int) $k)) {
            return $c. $ V. ' Before ';
       }
   }
}
?>

Example 3:

Copy Code code 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 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) {//3 days
Return floor ($dur/86400). ' Days ago ';
}else{
return $the _time;
}
}
}
}
}
}

Second, the realization of class

Copy Code code as follows:
<?php
/*
* Author:solon Ring
* time:2011-11-02
* Time calculation (year, month, day, time, minute, second)
* $createtime can be the current time
* $gettime the time you want 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 ()). "Month 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 classes
/*
*
* 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 ') );
*
*/

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.