Home >Backend Development >PHP Problem >How to get specific date in php

How to get specific date in php

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-06-01 17:15:112526browse

This article will introduce to you how to get a specific date in php. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to get specific date in php

Monday this week

// 本周一,w为星期几的数字形式,这里0为周日。
$dt = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));

Sunday this week

// 本周日,同样使用w,以现在与周日相关天数算。
$dt = date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600));

Monday last week

// 上周一,无论今天几号,-1 monday为上一个有效周未。
$dt = date('Y-m-d', strtotime('-1 monday', time()));

Last Sunday

// 上周日,上一个有效周日,同样适用于其它星期。
$dt = date('Y-m-d', strtotime('-1 sunday', time()));

First day of this month

// 本月1日,直接以strtotime生成
$dt = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00'));

Last day of this month

// 本月最后一天,t为当月天数,28至31天。
$dt = date('Y-m-d', strtotime(date('Y-m', time()) . '-' . date('t', time()) . ' 00:00:00'));

First day of last month

// 上月1日,本月1日上减一个月。
$dt = date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));

Last day of last month

// 上月最后一天,本月第一天减一天即是上月最后一天。
$dt = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400);

Recommended learning: php video tutorial

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

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete