Laravel 5 Want to customize the global function, how to do it?

Source: Internet
Author: User
Want to put

Generate Friendly time form function Friendly_date ($from) {static $now = NULL;    $now = = NULL && $now = time (); !    Is_numeric ($from) && $from = Strtotime ($from);    $seconds = $now-$from;    $minutes = Floor ($seconds/60);    $hours = Floor ($seconds/3600);    $day = Round ((strtotime (' y-m-d ', $now))-Strtotime (Date (' y-m-d ', $from))/86400);    if ($seconds = = 0) {return ' just ';    } if (($seconds >= 0) && ($seconds <=) {return "{$seconds} seconds ago";    } if (($minutes >= 0) && ($minutes <=) {return "{$minutes} minutes ago";    } if (($hours >= 0) && ($hours <=) {return "{$hours} hours ago";    } if ((Date (' Y ')-date (' Y ', $from)) > 0) {return date (' y-m-d ', $from);        } switch ($day) {case 0:return date (' H:i today ', $from);                Break        Case 1:return Date (' Yesterday H:i ', $from); Break                Default://$day + = 1;        Return "{$day} days ago";    Break }}

Put it in the function library.

Reply content:

Want to put

Generate Friendly time form function Friendly_date ($from) {static $now = NULL;    $now = = NULL && $now = time (); !    Is_numeric ($from) && $from = Strtotime ($from);    $seconds = $now-$from;    $minutes = Floor ($seconds/60);    $hours = Floor ($seconds/3600);    $day = Round ((strtotime (' y-m-d ', $now))-Strtotime (Date (' y-m-d ', $from))/86400);    if ($seconds = = 0) {return ' just ';    } if (($seconds >= 0) && ($seconds <=) {return "{$seconds} seconds ago";    } if (($minutes >= 0) && ($minutes <=) {return "{$minutes} minutes ago";    } if (($hours >= 0) && ($hours <=) {return "{$hours} hours ago";    } if ((Date (' Y ')-date (' Y ', $from)) > 0) {return date (' y-m-d ', $from);        } switch ($day) {case 0:return date (' H:i today ', $from);                Break        Case 1:return Date (' Yesterday H:i ', $from); Break                Default://$day + = 1;        Return "{$day} days ago";    Break }}

Put it in the function library.

Create a new file under app/helpers/(the directory can be your own) functions.php
Add this method to the functions.php
And then in
Add in bootstrap/autoload.php

require __DIR__.'/../app/Helpers/functions.php';

or in
Increase in AutoLoad under Composer.json

"files": [    "app/Helpers/functions.php"]
..."autoload": {    "classmap": [        "database"    ],    "psr-4": {        "App\\": "app/"    },    "files": [        "app/helpers/functions.php"    ]},...

Reference: Creating a Helpers File

Self-made function one, for everyone to reference and improve

/** * Format time * @param mixed \carbon\carbon $dt/int $timestamp/string $date/string ' Now ' * @return date */function for Mat_time ($DT) {$format = [' between_one_minute ' = ' just ', ' before_minute ' = ' minutes ago ', ' aft        Er_minute ' ~ = ' minutes later ', ' Today ' = ' h:i ', ' Yesterday ' = ' Yesterday h:i ', ' Tomorrow ' = ' Tomorrow h:i ', ' default ' = ' n ' d Day h:i ', ' diff_year ' = ' Y '    Year N month D Day h:i ', ' error ' = ' time display error '];            Create Object if (Is_int ($DT)) {$dt = Carbon\carbon::createfromtimestamp ($DT); } else if (! $dt instanceof \carbon\carbon) {//Error time if ($dt = = ' 0000-00-00 00:00:00 ' | | $dt = = = ' 0 ') retur        n $format [' Error '];    $dt = new Carbon\carbon ($DT);    } $now = \carbon\carbon::now ();        Today if ($dt->istoday ()) {$diff _minute = Floor (ABS ($now->timestamp-$DT->timestamp)/60); $dIff_second = $now->timestamp-$dt->timestamp; Within one hour if ($diff _minute < 60) {//within one minute if ($diff _second < && $diff _second >            = 0) return $format [' Between_one_minute ']; return $diff _second < 0?        $diff _minute. $format [' After_minute ']: $diff _minute. $format [' Before_minute '];    } return $dt->format ($format [' Today ']);    }//Yesterday if ($dt->isyesterday ()) return $dt->format ($format [' Yesterday ']);    Tomorrow if ($dt->istomorrow ()) return $dt->format ($format [' Tomorrow ']);    Not this year, other time if ($dt->format (' y ')!== $now->format (' y ')) return $dt->format ($format [' diff_year ']); This year, the other time return $dt->format ($format [' Default ']);}
  • 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.