PHP: Get Date 7 Days Ago-One Week Ago

I use The SEO System everyday to get my pages to the top! --Richard

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.

Want a fresh, unique perspective on web technologies once a week? Subscribe to Richard Cummings now.

Other SEO, Blogging, and Technical Artilces You May Enjoy

, , ,

2 Responses to “PHP: Get Date 7 Days Ago-One Week Ago”

  1. Ubaid Raja Says:

    Change strtotime(‘-7 days’) to strtotime(“-7 days”).


  1. [...] you read the post PHP get date 7 days ago, you know that my project today called for querying a MySql database on a date 7 days in the past. [...]

Leave a Reply