search
Homephp教程PHP源码获取本周一,本周日,上周一,上周日,上月初,月底

本类主要用于获取制定时间所在的周一、周日、上周一、上周日、月初、月底、上月初、上月底

<?php
class datefl{
 
    /**
     * 本周一
     * @param int $timestamp 某个月的某一个时间戳,默认为当前时间
     * @param string $date 日期格式
     * @param bool|true $zero 时间是否归零到凌晨零点
     * @return bool|int|string
     */
    static public function thisMonday($timestamp=0,$date=&#39;&#39;,$zero=true){
        //如果未设置时间,则使用当前时间
        $timestamp || $timestamp = time();
        //周一的时间
        $time = $timestamp-86400*(date(&#39;N&#39;,$timestamp)-1);
        //归零处理
        $zero && $time = self::_zero($time);
        //如果设置了日期格式,按格式返回
        return $date ? date($date,$time) : $time;
 
    }
 
    /**
     * 本周日
     * @param int $timestamp 某个月的某一个时间戳,默认为当前时间
     * @param string $date 日期格式
     * @param bool|true $zero 时间是否归零到凌晨零点
     * @return bool|int|string
     */
    static public function thisSunday($timestamp=0,$date=&#39;&#39;,$zero=true){
        //如果未设置时间,则使用当前时间
        $timestamp || $timestamp = time();
        //周日
        $time = $timestamp+86400*(7-date(&#39;N&#39;,$timestamp));
        //归零处理
        $zero && $time = self::_zero($time);
        //如果设置了日期格式,按格式返回
        return $date ? date($date,$time) : $time;
    }
 
    /**
     * 上周一
     * @param int $timestamp 某个月的某一个时间戳,默认为当前时间
     * @param string $date 日期格式
     * @param bool|true $zero 时间是否归零到凌晨零点
     * @return bool|int|string
     */
    static public function lastMonday($timestamp=0,$date=&#39;&#39;,$zero=true){
        //如果未设置时间,则使用当前时间
        $timestamp || $timestamp = time();
        //上周同一时间
        $timestamp = $timestamp-86400*7;
        //周一的时间
        $time = $timestamp-86400*(date(&#39;N&#39;,$timestamp)-1);
        //归零处理
        $zero && $time = self::_zero($time);
        //如果设置了日期格式,按格式返回
        return $date ? date($date,$time) : $time;
    }
 
    /**
     * 上个星期天
     * @param int $timestamp 某个月的某一个时间戳,默认为当前时间
     * @param string $date 日期格式
     * @param bool|true $zero 时间是否归零到凌晨零点
     * @return bool|int|string
     */
    static public function lastSunday($timestamp=0,$date=&#39;&#39;,$zero=true){
        //如果未设置时间,则使用当前时间
        $timestamp || $timestamp = time();
        //上周同一时间
        $timestamp = $timestamp-86400*7;
        //周日
        $time = $timestamp+86400*(7-date(&#39;N&#39;,$timestamp));
        //归零处理
        $zero && $time = self::_zero($time);
        //如果设置了日期格式,按格式返回
        return $date ? date($date,$time) : $time;
 
    }
 
    /**
     * 这个月的第一天
     * @param int $timestamp 某个月的某一个时间戳,默认为当前时间
     * @param string $date 日期格式
     * @param bool|true $zero 时间是否归零到凌晨零点
     * @return bool|int|string
     */
    static public function monthFirstDay($timestamp=0,$date=&#39;&#39;,$zero=true){
        //如果未设置时间,则使用当前时间
        $timestamp || $timestamp = time();
        //周一的时间
        $time = mktime(date(&#39;H&#39;,$timestamp),date(&#39;i&#39;,$timestamp),date(&#39;s&#39;,$timestamp),date(&#39;m&#39;,$timestamp),1,date(&#39;Y&#39;,$timestamp));
        //归零处理
        $zero && $time = self::_zero($time);
        //如果设置了日期格式,按格式返回
        return $date ? date($date,$time) : $time;
    }
 
 
    /**
     * 这个月的最后一天
     * @param int $timestamp 某个月的某一个时间戳,默认为当前时间
     * @param string $date 日期格式
     * @param bool|true $zero 时间是否归零到凌晨零点
     * @return bool|int|string
     */
    static public function monthLastDay($timestamp=0,$date=&#39;&#39;,$zero=true){
        //如果未设置时间,则使用当前时间
        $timestamp || $timestamp = time();
        //周一的时间
        $time = mktime(date(&#39;H&#39;,$timestamp),date(&#39;i&#39;,$timestamp),date(&#39;s&#39;,$timestamp),date(&#39;m&#39;,$timestamp),date(&#39;t&#39;,$timestamp),date(&#39;Y&#39;,$timestamp));
        //归零处理
        $zero && $time = self::_zero($time);
        //如果设置了日期格式,按格式返回
        return $date ? date($date,$time) : $time;
    }
 
    /**
     * 上个月的第一天
     * @param int $timestamp 某个月的某一个时间戳,默认为当前时间
     * @param string $date 日期格式
     * @param bool|true $zero 时间是否归零到凌晨零点
     * @return bool|int|string
     */
    static public function lastMonthFirstDay($timestamp=0,$date=&#39;&#39;,$zero=true){
        //如果未设置时间,则使用当前时间
        $timestamp || $timestamp = time();
        //周一的时间
        $time = mktime(date(&#39;H&#39;,$timestamp),date(&#39;i&#39;,$timestamp),date(&#39;s&#39;,$timestamp),date(&#39;m&#39;,$timestamp)-1,1,date(&#39;Y&#39;,$timestamp));
        //归零处理
        $zero && $time = self::_zero($time);
        //如果设置了日期格式,按格式返回
        return $date ? date($date,$time) : $time;
 
    }
 
    /**
     * 上个月的最后一天
     * @param int $timestamp 某个月的某一个时间戳,默认为当前时间
     * @param string $date 日期格式
     * @param bool|true $zero 时间是否归零到凌晨零点
     * @return bool|int|string
     */
    static public function lastMonthLastDay($timestamp=0,$date=&#39;&#39;,$zero=true){
 
        //如果未设置时间,则使用当前时间
        !$timestamp || $timestamp = time();
        //上月第一天的当前时间
        $timestamp = mktime(date(&#39;H&#39;,$timestamp),date(&#39;i&#39;,$timestamp),date(&#39;s&#39;,$timestamp),date(&#39;m&#39;,$timestamp)-1,1,date(&#39;Y&#39;,$timestamp));
        //取得最后一天时间
        $time = mktime(date(&#39;H&#39;,$timestamp),date(&#39;i&#39;,$timestamp),date(&#39;s&#39;,$timestamp),date(&#39;m&#39;,$timestamp),date(&#39;t&#39;,$timestamp),date(&#39;Y&#39;,$timestamp));
        //归零处理
        $zero && $time = self::_zero($time);
        //如果设置了日期格式,按格式返回
        return $date ? date($date,$time) : $time;
 
    }
 
    /**
     * 时间归零,回到当天的00:00:00
     * @param $timestamp
     * @return int
     */
    static private function _zero($timestamp)
    {
        return strtotime(date(&#39;Y-m-d&#39;,$timestamp));
    }
 
} 
 
$t = time();
    echo &#39;<br>本周星期一:&#39;;
    echo datefl::thisMonday($t,&#39;Y-m-d H:i:s&#39;);
    echo &#39;<br>本周星期天:&#39;;
    echo datefl::thisSunday($t,&#39;Y-m-d H:i:s&#39;);
    echo &#39;<br>上周星期一:&#39;;
    echo datefl::lastMonday($t,&#39;Y-m-d H:i:s&#39;);
    echo &#39;<br>上周星期天:&#39;;
    echo datefl::lastSunday($t,&#39;Y-m-d H:i:s&#39;);
    echo &#39;<br>本月第一天:&#39;;
    echo datefl::monthFirstDay($t,&#39;Y-m-d H:i:s&#39;);
    echo &#39;<br>本月最后一天:&#39;;
    echo datefl::monthLastDay($t,&#39;Y-m-d H:i:s&#39;);
    echo &#39;<br>上月第一天:&#39;;
    echo datefl::lastMonthFirstDay($t,&#39;Y-m-d H:i:s&#39;);
    echo &#39;<br>上月最后一天:&#39;;
    echo datefl::lastMonthLastDay($t,&#39;Y-m-d H:i:s&#39;);
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

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use