PHP Custom Format Time sample code _php instance

Source: Internet
Author: User
such as: The time is exactly 5 minutes ago, then the corresponding timestamp will be formatted as 5 minutes ago, not much to say, directly affixed to the code:
Copy CodeThe code is as follows:
/**
* Format time
* @param integer $timestamp timestamp
* @param string $format dt= datetime d= Date t= time u= personalization other = Custom
* @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 is East eight, which is China's
$lang = Array (
' Before ' = ' before ',
' Day ' = ' days ',
' Yday ' = ' Yesterday ',
' Byday ' = ' the day before yesterday ',
' Hour ' = ' hours ',
' Half ' = ' half ',
' min ' = ' min ',
' sec ' = ' s ',
' 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;
}
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.