22 Votes

PHP: Date 1 week ago

Question by WebHorn | Last update on 2023-11-23 | Created on 2016-10-30

I would like to determine the date exactly one week before today in a PHP script.

Unfortunately, it is not that easy that you can just subtract 7 days from the current date, because you also have to consider changes of months and years. Does someone have an idea?

ReplyPositiveNegative
4Best Answer4 Votes

You can just use the function strtotime() and pass "-7 days" or "-1 week" to go back seven days respectively one week into the past:

$t = strtotime("-7 days");
echo date("d.m.Y", $t);

In this example, we are first storing the timestamp within the variable $t and then we are displaying the date in the format day.month.year using PHP's date() function.

$t = strtotime("-1 week");
echo date("d.m.Y", $t);

When using "-1 week" instead of "-7 days" the result is the same.
Last update on 2023-11-23 | Created on 2016-10-31

ReplyPositive Negative
Reply

Related Topics

PHP: Determine Week Number

Tutorial | 0 Comments

PHP: Current Date and Time

Tutorial | 0 Comments

Change Date of File

Tutorial | 0 Comments

VirtualBox: Change Date and Time

Tutorial | 10 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.