PHP: Get Date 7 Days Ago-One Week Ago

Richard CummingsQuick Solutions, Technologies/Solutions2 Comments

In this quick tech solution post, I will tell you how to get the PHP date 7 days ago, one week ago, or whenever you want.

As I have said many times, I write these posts for both of us: you and I. When you are implementing tech solutions everyday, you are more like to forget than remember. So, today, let us review how to get the PHP date from 7 days ago.

It is fairly easy to get a past date in PHP but, as always, remembering the syntax can be a bit of a challenge. So, here we go.

PHP: Get a Past Date

You can get any past date you want in PHP but, below, you will find the formula to get the date from 7 days/one week ago.

PHP: Get Date One Week Ago

Ok, here is a little file that you can upload to your web server (I recommend Hostgator for PHP/MySQL) for testing if you like. I have noted the line that gets you the date one week prior.

 <?
// get today's date
$today = date("Y-m-d");
echo "Today is $today.";
echo "<br /><br />";
$oneweekago=date('Y-m-d',strtotime('-7 days'));  // this gets one week ago in PHP
echo "One week ago, the date was $oneweekago.";
?>

Summary: Past Dates In PHP

I hope this quick little tidbit about how to get past dates in PHP has been helpful. Today, I had a quick project in which I needed to get the date from 7 days ago in PHP. Hopefully, I will remember I wrote this so I can refer to it in the future. 🙂

But wait, my project is not over. I am doing a MySQL query where I have to get information from 1 week ago but the date is stored in a date time field…so I tell you how to do a query for date in a MySQL datetime field.

2 Comments on “PHP: Get Date 7 Days Ago-One Week Ago”

  1. Pingback: PHP/MySQL: Query Date in DateTime Field | Richard Cummings

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.