search
HomeBackend DevelopmentPHP ProblemHow to get specific date in php

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. If there is any infringement, please contact admin@php.cn delete

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.