How to Find Tomorrow, Next Month, or Next Year using PHP

I found an interesting new way to find a future or past day, month, or year using PHP. It is much easier than using a timestamp and mktime() to calculate a new date. Since this method was so easy I thought I would share it with all 3 fans of my blog.

How this method works is by using the date() function and strtotime() to format a new date using a text date. I have tried it with months, days, and years and it works awesome.

Below are a few examples of this method in use. I am sure it can be expanded further than I have used it here but I just thought I would share.

<?PHP
 
     //TODAY'S DATE
     $start =  date("Y-m-d");
 
     //1 MONTH FROM TODAY
     $end = date("Y-m-d",strtotime("+1 months"));
 
     //SEND REMINDER 25 DAYS FROM TODAY
     $reminder = date("Y-m-d",strtotime("+25 days"));
 
     //REMOVE THE RECORD 1 YEAR FROM TODAY
     $remove = date("Y-m-d",strtotime("+1 years"));
 
?>

Start is today’s date. End is 1 month from today. Reminder is 25 days from today. I have tested this functionality and it works great. You don’t have to worry about doing math, working with a timestamp, or calculating the start or end of a year.

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

Thanks Ryan, This post was a great help.

I needed your next month code, but had to modify it slightly so that it worked recursively.

I used it like this

<?php
 
	$year = !empty($_GET['year'])? $_GET['year'] : date('Y');
	$month = !empty($_GET['month'])? $_GET['month'] : date('m');
	$previous =  date("Y/m",strtotime($year."-".$month."-01 -1 months"));
	$next =  date("Y/m",strtotime($year."-".$month."-01 +1 months"));
 
?>

That’s what I searched for, thank you! :)

very cool… thank you so much for you guys’s help…

i was looking for a script to get next month last year…
here is my version based on your work:


Sales next month last year:

very cool… thank you so much for you guys’s help…

i was looking for a script to get next month last year…
here is my version based on your work:

Sales next month last year:

Nicely done. I hate using mktime() and all the crap that goes with it. This is great. One suggestion… your code example should pass $start to each of the other calls, like this:

$end = date (“Y-m-d”, strtotime ($start . “+1 months”));

This way it’s easy to update $start to a date other than today.

Thanks!

Thanks man!

From now on, there are 4 of them ;)

I think there is a problem with this:
$end = date(“Y-m-d”,strtotime(“+1 months”));

if you are on 2009-01-31
you get: 2009-03-03

I think this is because “+1 months” just add 31 days.

To get the next month, I have done this function:

function get_next_month($tstamp) {
return (strtotime(‘+1 months’, strtotime(date(‘Y-m-01′, $tstamp))));
}

(Here the day is set to the first of each month)

With that, we can easily get a list of month between to timestamps:

[code]
$start = strtotime('2009-01-31');
$end = time(); // <- Current year
$months = array;
for ($i = $start; $i <= $end; $i = get_next_month($i)) {
$months[] = $i; // <- Timestamp with each first day of month
}
[/code]

Hi Ryan, this is different to what I have seen before. Been doin php for a few years now, but will probably never completely master all there is to know about dates. Thanks, will use this code now!

Thanks. This is so helpful.

thanks @Glide for your little function.

Struggled a bit with “give me next months first date if I have timestamp”.

the solution was simple:

$nextmonthsfirstdate =  date('d.m.Y',strtotime('next month'));

Mediawrox:
That is wrong because when you will be on 31.01.2010 you will get 03.03.2010.

It is because next month just add 31 days to the today date.

anarkae: You are right. Fixed below. Right?

[code]
$date=("d.m.Y");
$date=strtotime($date);
$date=date('Y-m-1',$date);
$now=strtotime("+1 month", strtotime($date));

$lastdayofnextmonth=strtotime("-1 day", $now);
$nextmonthsfirstdate = date('m.d.Y',$now);
[/code]

Hey guys this is nice script

Leave a comment

(required)

(required)