02 Votes

PHP: Date before X days

Question by CSchaf | Last update on 2020-10-20 | Created on 2016-11-01

Is there any possibility to calculate with dates in PHP?

I would like to subtract some days from the current date, for example to determine the date of yesterday, the date 10 days ago or the date 100 days ago. Can someone help me?

ReplyPositiveNegative
4Best Answer4 Votes

You can do that quite easy using the PHP function strtotime(). You can pass a date in text form to this function and you get a timestamp back.

You can even work with time constants such as day, week, month or year. Here are some examples for you:

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

This outputs the date of yesterday, that is the date one day back in past.

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

This outputs the date before one week, that is the date before seven days.

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

Using this code, you get the date ten days ago.

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

And using this code, you get the date one hundred days ago.
2016-11-01 at 23:52

ReplyPositive Negative
Reply

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

PHP: Determine Week Number

Tutorial | 0 Comments

VirtualBox: Change Date and Time

Tutorial | 10 Comments

Change Date of File

Tutorial | 0 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.