Categories: Php

PHP date() and strtotime() return wrong months on 31st

Share

On 31st of every month you will get a wrong month value while using date() or strtotime() functions.

In that case you need to pass the date of the first day of the current month as a second parameter to the strtotime() function.

For example:

$base_month = strtotime(date('Y-m',time()) . '-01 00:00:01');
echo date('Y-m',strtotime('-1 months', $base_month));
echo date('Y-m',strtotime('-2 months', $base_month));
echo date('Y-m',strtotime('-3 months', $base_month));

Recent Posts

Recover From Google Penalties in 10 Steps in 2024

Facing Google penalties can be a daunting challenge for businesses relying on organic search traffic.…

10 hours ago

What is the Future of Artificial Intelligence?

Technology, undeniably, has made our lives easier. But the advent of artificial intelligence has been…

13 hours ago

Role of guest post services in SEO

Guest post services have become an essential tool for businesses and individuals looking to increase…

21 hours ago

Top Programming Trends You Need to Know in 2024

Programming continues to evolve at a rapid pace, with new trends and technologies emerging every…

1 day ago

Why startups need a digital marketing strategy?

Let’s be honest; a startup business hardly has sufficient amount to promote their services and…

2 days ago

How to Find the Best SEO Consultant in Toronto to Boost Your Rankings

Introduction In today's digital age, having a strong online presence is crucial for businesses in…

2 days ago