Home  >  Article  >  Backend Development  >  PHP custom formatted time sample code_PHP tutorial

PHP custom formatted time sample code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:17:59852browse

For example: if the time is exactly 5 minutes ago, the corresponding timestamp will be formatted as 5 minutes ago. Without further ado, just paste the code:

Copy code The code is as follows:

/**
* Format time
* @param integer $timestamp timestamp
* @param string $format dt=date time d=date t=time u=personalized other=customized
* @ param integer $timeoffset time zone value
* @param string $custom_format custom time 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; //The default here is East Eighth District, which is Beijing time
$lang = array(
'before' => 'before',
'day' => 'day',
'yday' => 'yesterday',
'byday' => 'the day before yesterday',
' hour' => 'hour',
'half' => 'half',
'min' => 'minute',
'sec' => 'second',
'now' => 'just',
);
$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 = '' . intval( $time / 3600 ) . $lang['hour'] . $lang['before'] . '';
} elseif ( $time > 1800 ) {
$return = '' . $lang['half'] . $lang['hour'] . $lang['before'] . ' ';
} elseif ( $time > 60 ) {
$return = '' . intval( $time / 60 ) . $lang['min'] . $lang['before'] . '';
} elseif ( $time > 0 ) {
$return = '' . $time . $lang['sec'] . $lang['before'] . '';
} elseif ( $time == 0 ) {
$return = '' . $lang['now'] . '';
} else {
$return = $s;
}
} elseif ( ($days = intval( ($todaytimestamp - $timestamp) / 86400 )) >= 0 && $days < 7 ) {
if ( $days == 0 ) {
$return = '' . $lang['yday'] . gmdate( $time_format, $timestamp ) . '';
} elseif ( $days == 1 ) {
$return = '' . $lang['byday '] . gmdate( $time_format, $timestamp ) . '';
} else {
$return = '' . ($days + 1) . $lang['day'] . $lang['before'] . '';
}
} else {
$return = $s;
}
} else {
$return = gmdate( $format, $timestamp );
}
return $return;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621695.htmlTechArticleFor example: the time is exactly 5 minutes ago, then the corresponding timestamp will be formatted as 5 minutes ago. Without further ado, just paste the code: Copy the code as follows: /** *Formatting time* @par...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn