Home php教程 php手册 php:自定义的格式化时间方法

php:自定义的格式化时间方法

Jun 13, 2016 am 10:19 AM
php: format customize

如:时间刚好是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;
    }
Copy after login


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to quickly set up a custom avatar in Netflix How to quickly set up a custom avatar in Netflix Feb 19, 2024 pm 06:33 PM

An avatar on Netflix is ​​a visual representation of your streaming identity. Users can go beyond the default avatar to express their personality. Continue reading this article to learn how to set a custom profile picture in the Netflix app. How to quickly set a custom avatar in Netflix In Netflix, there is no built-in feature to set a profile picture. However, you can do this by installing the Netflix extension on your browser. First, install a custom profile picture for the Netflix extension on your browser. You can buy it in the Chrome store. After installing the extension, open Netflix on your browser and log into your account. Navigate to your profile in the upper right corner and click

How to format c drive with dos command How to format c drive with dos command Feb 19, 2024 pm 04:23 PM

DOS command is a command line tool used in Windows operating system, which can be used to perform various system management tasks and operations. One of the common tasks is to format the hard drive, including the C drive. Formatting the C drive is a relatively dangerous operation because it will erase all data on the C drive and reinitialize the file system. Before performing this operation, make sure you have backed up important files and have a clear understanding of the impact that formatting will have on your computer. The following is formatted in the DOS command line

How to create and customize Venn diagrams in Python? How to create and customize Venn diagrams in Python? Sep 14, 2023 pm 02:37 PM

A Venn diagram is a diagram used to represent relationships between sets. To create a Venn diagram we will use matplotlib. Matplotlib is a commonly used data visualization library in Python for creating interactive charts and graphs. It is also used to create interactive images and charts. Matplotlib provides many functions to customize charts and graphs. In this tutorial, we will illustrate three examples to customize Venn diagrams. The Chinese translation of Example is: Example This is a simple example of creating the intersection of two Venn diagrams; first, we imported the necessary libraries and imported venns. Then we create the dataset as a Python set, after that we use the "venn2()" function to create

How to customize shortcut key settings in Eclipse How to customize shortcut key settings in Eclipse Jan 28, 2024 am 10:01 AM

How to customize shortcut key settings in Eclipse? As a developer, mastering shortcut keys is one of the keys to improving efficiency when coding in Eclipse. As a powerful integrated development environment, Eclipse not only provides many default shortcut keys, but also allows users to customize them according to their own preferences. This article will introduce how to customize shortcut key settings in Eclipse and give specific code examples. Open Eclipse First, open Eclipse and enter

The operation process of edius custom screen layout The operation process of edius custom screen layout Mar 27, 2024 pm 06:50 PM

1. The picture below is the default screen layout of edius. The default EDIUS window layout is a horizontal layout. Therefore, in a single-monitor environment, many windows overlap and the preview window is in single-window mode. 2. You can enable [Dual Window Mode] through the [View] menu bar to make the preview window display the playback window and recording window at the same time. 3. You can restore the default screen layout through [View menu bar>Window Layout>General]. In addition, you can also customize the layout that suits you and save it as a commonly used screen layout: drag the window to a layout that suits you, then click [View > Window Layout > Save Current Layout > New], and in the pop-up [Save Current Layout] Layout] enter the layout name in the small window and click OK

Revealed secrets of cell phone format recovery methods (mobile phone malfunction? Don't worry) Revealed secrets of cell phone format recovery methods (mobile phone malfunction? Don't worry) May 04, 2024 pm 06:01 PM

Nowadays, we will inevitably encounter some problems such as being unable to turn on the phone or lagging, such as system crash, but during use, mobile phones have become an indispensable part of our lives. We are often at a loss, and sometimes, there are no solutions to these problems. To help you solve cell phone problems, this article will introduce you to some methods of cell phone format recovery and restore your phone to normal operation. Back up data - protect important information, such as photos and contacts, from being lost during the formatting process. Before formatting your phone, the first thing to consider is to back up important data and files on your phone. To ensure data security, or choose to transfer files to a cloud storage service, you can back it up by connecting to a computer. Use the system's built-in recovery function - simple

Will formatting a laptop make it faster? Will formatting a laptop make it faster? Feb 12, 2024 pm 11:54 PM

Will formatting a laptop make it faster? If you want to format your Windows laptop but want to know if it will make it faster, this article will help you know the right answer to this question. Will formatting a laptop make it faster? There are many reasons why users format their Windows laptops. But the most common reason is slow performance or speed of your laptop. Formatting a laptop will completely delete all data stored on the C drive or the hard drive partition where Windows operating system is installed. Therefore, every user will think twice before taking this step, especially when it comes to the performance of the laptop. This article will help you understand whether formatting your laptop will speed it up. Formatting your laptop helps

Computer formatting tutorial Computer formatting tutorial Jan 08, 2024 am 08:21 AM

Many times when using a computer, you will encounter too much garbage, but many users still don’t know how to format the computer. It doesn’t matter. Here is a tutorial on computer formatting for you to take a look at. How to format a computer: 1. Right-click "This PC" on the desktop and click "Manage". 2. Click "Storage" in "Computer Management" to open "Disk Management". 3. Select the hard drive you want to clean, right-click and select "Format". 4. Check "Perform Quick Format" and click "OK" to start formatting.

See all articles