Thursday, December 1, 2011

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.
No related posts.
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"));
 
?>
 
reference 
http://www.stemkoski.com/how-to-find-tomorrow-next-month-or-next-year-using-php/#comment-95499 
 

MS in Computer Science with paid training in USA company