


PHP calculates several functions and classes shared minutes ago, hours ago, and days ago_PHP Tutorial
1. Function implementation
Example 1:
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 return $the_time;
}else{
if($dur return $dur.'seconds ago';
}else{
if($dur return floor($dur/60).'minutes ago';
}else{
}else{
if($dur return floor($dur/3600).'hours ago';
}else{
if($dur return floor($dur/86400).'days ago'; 🎜>
Example 2:
The code is as follows:
'604800'=>'week',
'86400'=>'day',
'3600'=>'hour',
'60'=> 'Minutes',
'1'=>'Seconds'
);
foreach ($f as $k=>$v) {
if (0 !=$c=floor ($ t/(int) $ k)) {
Return $ C. $ v.
Example 3:
Copy code
The code is as follows:
$timer = strtotime($date);
$diff = $_SERVER['REQUEST_TIME'] - $timer;
$free = $free % 3600;
if($hour>0){
return $hour."hour 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
The code is as follows:
$now_time = strtotime($now_time);
$show_time = strtotime($the_time);
if($dur return floor($dur/60).'minutes ago';
}else{
if($dur return floor($dur/3600).'hours ago';
}else{
if($dur return floor($dur/86400).'day';
}else{
return $the_time;
}
}
}
}
}
}
2. Class implementation
Copy code
The code is as follows:
* author: Solon Ring
* time: 2011-11-02
* 发博时间计算(年,月,日,时,分,秒)
* $createtime 可以是当前时间
* $gettime 你要传进来的时间
*/
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())." 年前";
exit();
}
if($this->getMonth() > 1)
{
return intval($this->getMonth())." 月前";
exit();
}
if($this->getDay() > 1)
{
return intval($this->getDay())." 天前";
exit();
}
if($this->getHours() > 1)
{
return intval($this->getHours())." 小时前";
exit();
}
exit();
}
exit() ;
}
}
}
//Usage examples of the class
/*
* Call the class output method
* * $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
true

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
