Home >Backend Development >PHP Tutorial >PHP very practical time acquisition example analysis
In our two previous articlesThe specific process of formatting time in phpandphp to get the examples of last Monday, last Sunday, and next Monday, we just A separate function about time acquisition and formatting is introduced separately. I believe that many friends will be familiar with and use it to obtain time.
So today we will introduce to you a complete time acquisition, involving PHP operation skills for obtaining and comparing dates, times, days, etc. It is very simple and practical. Let’s start our course!
The first step is to download the very practical php time library we need for this course: http://www.php.cn/xiazai/leiku/615
Step 2: After the download is completed, find the php class file we need, unzip it to our local directory, and create a new php file!
The third step, after completion, we need to call this class in the new php file and instantiate the class:
<?php include_once "date.php";//引入类文件 $obj = new time; //实例化 $time=""; //输出年月日,时分秒,礼拜 echo "年份:".$obj->getyear($time)."<br>"; echo "月份:".$obj->getmonth($time)."<br>"; echo "当天:".$obj->getday($time)."<br>"; echo "小时:".$obj->gethour($time)."<br>"; echo "分钟:".$obj->getminute($time)."<br>"; echo " 秒:".$obj->getsecond($time)."<br>"; echo "礼拜:".$obj->getweekday($time)."<br>"; ////比较两个时间的差值 $time1="2017-8-17 11:25:30"; $time2="2017-8-7 12:05:55"; echo "返回数值:".$obj->compare($time1,$time2)."<br>"; //第一个时间大于等于第二个时间 返回1 否则返回0 //比较两个时间的差值 echo $obj->diffdate($time1,$time2)."<br>"; //返回 X年X月X日 echo $obj->buildDate($time,$time)."<br>"; ?>
Run the file, and the result will be as shown below:
The above is the detailed content of PHP very practical time acquisition example analysis. For more information, please follow other related articles on the PHP Chinese website!