php获取时间的几种方法
Libérer: 2016-07-25 09:05:29
original
1119 Les gens l'ont consulté
-
- function getWeekName($data,$format = '星期')
- {
- $week = date( "D ",$data);
- switch($week)
- {
- case "Mon ":
- $current = $format."一";
- break;
- case "Tue ":
- $current = $format."二";
- break;
- case "Wed ":
- $current = $format."三";
- break;
- case "Thu ":
- $current = $format."四";
- break;
- case "Fri ":
- $current = $format."五";
- break;
- case "Sat ":
- $current = $format."六"; break;
- case "Sun ":
- $current = $format."日";
- break;
- }
- return $current;
- }
-
- echo '今天是:'.getWeekName(time(),'星期');
- echo '
';
- echo '今天是:'.getWeekName(time(),'礼拜');
- echo '
';
- echo '2010-12-12是:'.getWeekName(strtotime('2010-12-12'),'礼拜');
- ?>
复制代码
4、获取类似文章发表的几小时前等效果的自定义函数
-
- function time2Units ($time)
- {
- $year = floor($time / 60 / 60 / 24 / 365);
- $time -= $year * 60 * 60 * 24 * 365;
- $month = floor($time / 60 / 60 / 24 / 30);
- $time -= $month * 60 * 60 * 24 * 30;
- $week = floor($time / 60 / 60 / 24 / 7);
- $time -= $week * 60 * 60 * 24 * 7;
- $day = floor($time / 60 / 60 / 24);
- $time -= $day * 60 * 60 * 24;
- $hour = floor($time / 60 / 60);
- $time -= $hour * 60 * 60;
- $minute = floor($time / 60);
- $time -= $minute * 60;
- $second = $time;
- $elapse = '';
-
- $unitArr = array('年' =>'year', '个月'=>'month', '周'=>'week', '天'=>'day',
- '小时'=>'hour', '分钟'=>'minute', '秒'=>'second'
- );
-
- foreach ( $unitArr as $cn => $u )
- {
- if ( $$u > 0 )
- {
- $elapse = $$u . $cn;
- break;
- }
- }
-
- return $elapse;
- }
-
- $past = 2052345678; // 发布日期
- $now = time(); // 当前日期
- $diff = $now - $past;//相差值
-
- echo '发表于' . time2Units($diff) . '前';
- ?>
复制代码
另一种,个人认为比较好的:
-
-
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.'秒前';
- }else{
- if($dur return floor($dur/60).'分钟前';
- }else{
- if($dur return floor($dur/3600).'小时前';
- }else{
- if($dur return floor($dur/86400).'天前';
- }else{
- return $the_time;
- }
- }
- }
- }
- }
- }
- ?>
复制代码
5、根据两时间差具体算相差时间
-
-
function getTime( $val ){
- if($val>0){
- $nTime['nDay'] = (int)($val/(3600*24));
- $nTime['nHour'] = (int)($val%(3600*24)/3600);
- $nTime['nMin'] = (int)($val%(3600*24)%3600/60);
- $nTime['nSec'] = (int)($val%(3600*24)%3600%60);
- }
- return $nTime ;
- }
- function getStrTime( $val ){
- $aTime = getTime($val);
- $dtoc = array('nDay'=>'天','nHour'=>'小时','nMin'=>'分','nSec'=>'秒');
- if( $aTime ){
- foreach( $aTime as $k=>$v){
- if($v){
- $cTime .= $v.$dtoc[$k];
- }
- }
- }else{
- $cTime = '已结止';
- }
- return $cTime;
- }
- ?>
复制代码
|
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31