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

How to get specific date in php

醉折花枝作酒筹
Release: 2023-03-10 08:34:01
forward
2528 people have browsed it

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));
Copy after login

Sunday this week

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

Monday last week

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

Last Sunday

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

First day of this month

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

Last day of this month

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

First day of last month

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

Last day of last month

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

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!

Related labels:
php
source:segmentfault.com
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