How to format time to Chinese in PHP

PHPz
Release: 2023-03-29 15:25:55
Original
708 people have browsed it

In web application development, time handling is a very important issue, and time formatting is often required to display the correct time on the page. For PHP developers, time processing and formatting are very common and necessary operations. However, for a website that uses Chinese as the main language, how to format the time into Chinese becomes very important. This article will introduce how to format time into Chinese in PHP.

Time functions in PHP

There are many date and time functions in PHP, such as date() and time(), etc. These functions allow us to get the current date and time data in a PHP script. For example, we can use the following code to get the current date and time:

$date = date('Y-m-d H:i:s');
echo $date;
Copy after login

This will output the current date and time, for example: "2021-01-01 10:30:00".

Format PHP time into Chinese time

For a website using Chinese, we need to format the current time into Chinese time. The following is a PHP function that formats time into Chinese:

function cn_date($format, $timestamp=null){
  static $now;
  if(!isset($now)) $now=time();
  if(!$timestamp) $timestamp=$now;
  $format=str_replace(array('周','星期'),array('周','周'),$format);
  $cn_week=array('日','一','二','三','四','五','六');
  $en_week=array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  $days=array('今天','明天','后天');
  $diff_day=ceil(($timestamp-strtotime('today 00:00'))/86400);
  if($diff_day<3) return $days[$diff_day].' '.date('H:i',$timestamp);
  $week=date('w',$timestamp);
  $cn_time=['凌晨','早上','上午','中午','下午','晚上','深夜'];
  $en_time=['0-5','6-8','9-11','12-13','14-18','19-21','22-24'];
  $time_arr=array_combine($en_time,$cn_time);
  $time_str=date('H',$timestamp).':'.str_pad(floor(date('i',$timestamp)/10)*10,2,'0');
  $time_info=$time_arr[floor(date('H',$timestamp)/3)*3];
  $cn_weekday='周'. $cn_week[$week];
  $en_weekday=$en_week[$week];
  return str_replace(array('W','w','D','d','H','h','I','i','S','s','AW','aw'),
  array($cn_weekday,$en_weekday,date('n',$timestamp),date('j',$timestamp),$time_str,
  date('g',$timestamp),$time_info,date('i',$timestamp),'秒',$diff_day.'天',$diff_day), $format);
}
Copy after login

This function formats time into Chinese time and outputs different information based on different dates. For example, the current time is October 10, 2021, 13:30:00. After formatting, the output is "Today 01:30 PM".

Usage example:

echo cn_date('Y年m月d日 w H:i:s', time());
Copy after login

This will output a formatted version of the current time, for example: "Sunday, October 10, 2021 13:30:00".

Conclusion

Formatting time to Chinese in PHP is very simple, we just need to use the appropriate functions and format strings. This article provides an effective PHP function that can format time into Chinese, and the output information is very practical. If you are developing a Chinese website, then this function should be perfect for you.

The above is the detailed content of How to format time to Chinese in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template