Skip to content

PHP Yesterday Date or other Date in Past or Future

by Jon on January 4th, 2010

When using php and mysql for reporting it is common to have to get yesterday’s date or even a week ago to get the correct results from the report. So to make it easy for the user I will pre-populate the date fields with yesterday or an older date. This is pretty easy to do and there are a couple ways to achieve the same thing.

Here is an example of 2 different ways to do it, this will give you 7 days before the current date.

$weekbefore = mktime(0, 0, 0, date(“m”), date(“d”)-7, date(“y”));
$weekbefore = date( strtotime(“-7 days”));

This can then be broken down into the month, day, and year with the following code.

$month1 = date(“n”,$weekbefore);
$day1 = date(“j”,$weekbefore);
$year1 = date(“Y”,$weekbefore);

This will give you the ability to preselect the date selectors on your report web pages so users will not have to but they will have the option to make the date whatever the want if needed.

These examples will also work for dates in the future, so say 3 days from now.

$threedaysfromnow = mktime(0, 0, 0, date(“m”), date(“d”)+3, date(“y”));
$threedaysfromnow = date( strtotime(“+3 days”));

From → Tech

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS

%d bloggers like this: