无为清净楼资源网 Design By www.qnjia.com
如:时间刚好是5分钟前,则对应的时间戳就会被格式化为5分钟前,不多说了,直接贴上代码:
复制代码 代码如下:
/**
* 格式化时间
* @param integer $timestamp 时间戳
* @param string $format dt=日期时间 d=日期 t=时间 u=个性化 其他=自定义
* @param integer $timeoffset 时区值
* @param string $custom_format 自定义时间格式
* @return string
*/
public function dgmdate( $timestamp, $format = 'dt', $timeoffset = '9999', $custom_format = '' ) {
$return = '';
$now = time();
$day_format = 'Y-n-j';
$time_format = 'H:i:s';
$date_format = $day_format . ' ' . $time_format;
$offset = 8; //这里默认是东八区,也就是北京时间
$lang = array(
'before' => '前',
'day' => '天',
'yday' => '昨天',
'byday' => '前天',
'hour' => '小时',
'half' => '半',
'min' => '分钟',
'sec' => '秒',
'now' => '刚刚',
);
$timeoffset = $timeoffset == 9999 ? $offset : $timeoffset;
$timestamp += $timeoffset * 3600;
switch ( $format ) {
case 'dt':
$format = $date_format;
break;
case 'd':
$format = $day_format;
break;
case 't':
$format = $time_format;
break;
}
if ( $format == 'u' ) {
$todaytimestamp = $now - ($now + $timeoffset * 3600) % 86400 + $timeoffset * 3600;
$s = gmdate( empty( $custom_format ) ? $date_format : $custom_format, $timestamp );
$time = $now + $timeoffset * 3600 - $timestamp;
if ( $timestamp >= $todaytimestamp ) {
if ( $time > 3600 ) {
$return = '<span title="' . $s . '">' . intval( $time / 3600 ) . $lang['hour'] . $lang['before'] . '</span>';
} elseif ( $time > 1800 ) {
$return = '<span title="' . $s . '">' . $lang['half'] . $lang['hour'] . $lang['before'] . '</span>';
} elseif ( $time > 60 ) {
$return = '<span title="' . $s . '">' . intval( $time / 60 ) . $lang['min'] . $lang['before'] . '</span>';
} elseif ( $time > 0 ) {
$return = '<span title="' . $s . '">' . $time . $lang['sec'] . $lang['before'] . '</span>';
} elseif ( $time == 0 ) {
$return = '<span title="' . $s . '">' . $lang['now'] . '</span>';
} else {
$return = $s;
}
} elseif ( ($days = intval( ($todaytimestamp - $timestamp) / 86400 )) >= 0 && $days < 7 ) {
if ( $days == 0 ) {
$return = '<span title="' . $s . '">' . $lang['yday'] . gmdate( $time_format, $timestamp ) . '</span>';
} elseif ( $days == 1 ) {
$return = '<span title="' . $s . '">' . $lang['byday'] . gmdate( $time_format, $timestamp ) . '</span>';
} else {
$return = '<span title="' . $s . '">' . ($days + 1) . $lang['day'] . $lang['before'] . '</span>';
}
} else {
$return = $s;
}
} else {
$return = gmdate( $format, $timestamp );
}
return $return;
}
复制代码 代码如下:
/**
* 格式化时间
* @param integer $timestamp 时间戳
* @param string $format dt=日期时间 d=日期 t=时间 u=个性化 其他=自定义
* @param integer $timeoffset 时区值
* @param string $custom_format 自定义时间格式
* @return string
*/
public function dgmdate( $timestamp, $format = 'dt', $timeoffset = '9999', $custom_format = '' ) {
$return = '';
$now = time();
$day_format = 'Y-n-j';
$time_format = 'H:i:s';
$date_format = $day_format . ' ' . $time_format;
$offset = 8; //这里默认是东八区,也就是北京时间
$lang = array(
'before' => '前',
'day' => '天',
'yday' => '昨天',
'byday' => '前天',
'hour' => '小时',
'half' => '半',
'min' => '分钟',
'sec' => '秒',
'now' => '刚刚',
);
$timeoffset = $timeoffset == 9999 ? $offset : $timeoffset;
$timestamp += $timeoffset * 3600;
switch ( $format ) {
case 'dt':
$format = $date_format;
break;
case 'd':
$format = $day_format;
break;
case 't':
$format = $time_format;
break;
}
if ( $format == 'u' ) {
$todaytimestamp = $now - ($now + $timeoffset * 3600) % 86400 + $timeoffset * 3600;
$s = gmdate( empty( $custom_format ) ? $date_format : $custom_format, $timestamp );
$time = $now + $timeoffset * 3600 - $timestamp;
if ( $timestamp >= $todaytimestamp ) {
if ( $time > 3600 ) {
$return = '<span title="' . $s . '">' . intval( $time / 3600 ) . $lang['hour'] . $lang['before'] . '</span>';
} elseif ( $time > 1800 ) {
$return = '<span title="' . $s . '">' . $lang['half'] . $lang['hour'] . $lang['before'] . '</span>';
} elseif ( $time > 60 ) {
$return = '<span title="' . $s . '">' . intval( $time / 60 ) . $lang['min'] . $lang['before'] . '</span>';
} elseif ( $time > 0 ) {
$return = '<span title="' . $s . '">' . $time . $lang['sec'] . $lang['before'] . '</span>';
} elseif ( $time == 0 ) {
$return = '<span title="' . $s . '">' . $lang['now'] . '</span>';
} else {
$return = $s;
}
} elseif ( ($days = intval( ($todaytimestamp - $timestamp) / 86400 )) >= 0 && $days < 7 ) {
if ( $days == 0 ) {
$return = '<span title="' . $s . '">' . $lang['yday'] . gmdate( $time_format, $timestamp ) . '</span>';
} elseif ( $days == 1 ) {
$return = '<span title="' . $s . '">' . $lang['byday'] . gmdate( $time_format, $timestamp ) . '</span>';
} else {
$return = '<span title="' . $s . '">' . ($days + 1) . $lang['day'] . $lang['before'] . '</span>';
}
} else {
$return = $s;
}
} else {
$return = gmdate( $format, $timestamp );
}
return $return;
}
标签:
php,格式化时间
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 张真.1993-失恋十四行【上华】【WAV+CUE】
- 钟明秋《爱有天意HQCD》[低速原抓WAV+CUE]
- 孙云岗唢呐《金声玉振[HIFI珍藏版]》[低速原抓WAV+CUE]
- 谭艳精讯TEST-CD试音39号》2CD[DTS-WAV]
- 姚璎格《发烧女中音》DSD版[低速原抓WAV+CUE][1G]
- 张玮伽《微风细雨DSD》发烧大碟[WAV+CUE][1.1G]
- 群星《2024好听新歌14》十倍音质 U盘音乐 [WAV分轨][966M]
- s14全球总决赛T1战队队员都有谁 LOLs14全球总决赛T1战队介绍
- 英雄联盟faker身价有10亿吗 英雄联盟faker身价介绍一览
- faker大魔王称号怎么来的 faker大魔王称号来源介绍
- PS5 Pro上的蒂法更美了!博主盛赞新机1000%值得购买
- 腾讯互娱再离职一员大将!或因供应商贪腐
- Ayaneo3游戏掌机预热:旗舰定位、造型圆润自带底键
- 动力火车.1999-背叛情歌【上华】【WAV+CUE】
- 刘力扬.2019-Neon.Lit虹【摩登天空】【FLAC分轨】