Home >Backend Development >PHP Tutorial >PHP gets summary of various times (code)

PHP gets summary of various times (code)

不言
不言Original
2018-08-21 16:25:582966browse

The content of this article is a summary (code) of various times obtained by PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Get the system time

For example: the day before the system, the previous day, the current Monday time of the previous week, etc.

1. Get the current system time
echo "date('Y-m-d',time())";
2. Get the system time of the previous day
echo "date("Y-m-d",strtotime("-1 day"))";
需要前几天就减去(-) 需要几天后就加(+)
3. Get the Monday and Sunday dates of the previous week in the current system time
//上周周日的日期
$lastSunday = date('Y-m-d', strtotime('-1 sunday', time()));
//根据时间差 减去6天前,算出上周周一日期
$lastMonday = date('Y-m-d',strtotime('$lastSunday -6 day',$ss));
4. Get the day of the current week. Sunday is 0, Monday to Saturday is 1 - 6
    $today = date('Y-m-d',time());
    //获取当前周的第几天 周日是 0 周一到周六是 1 - 6  
    $w=date('w',strtotime($today));  
    $first=1;
    //获取本周开始日期,如果$w是0,则表示周日,减去 6 天     
    $weekStart=date('Y-m-d 00:00:00',strtotime("$today-".($w ? $w - $first : 6).' days'));            
    //本周结束日期  
    $weekEnd=date('Y-m-d 23:59:59',strtotime("$weekStart +6 days"));
    //前一周的起始时间
    $weekStart = date('Y-m-d 00:00:00',strtotime("$weekStart -7 days"));
    $weekEnd = date('Y-m-d 23:59:59',strtotime("$weekEnd -7 days")););
5. How many days were there in the previous month
$t = date('t',strtotime('-1 month'));

Related recommendations:

The implementation code of ftp upload and download function in php

php implements file upload and download Method (code)

The above is the detailed content of PHP gets summary of various times (code). For more information, please follow other related articles on the PHP Chinese website!

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