开发者

How to calculate and get a date in the past (e.g. 3 weeks ago) from today's date (PHP)

开发者 https://www.devze.com 2023-02-02 04:41 出处:网络
I have a news filtering script and I use this usually to get the current day: echo date(\'j n Y\'); 开发者_Python百科

I have a news filtering script and I use this usually to get the current day:

echo date('j n Y');
开发者_Python百科

Which returns something like 1 1 2011. What I want to do is being able to return the date a week ago relative to current date/day so that it would return just example (25 12 2010) if I choose to get 1 week ago. And so on if I want to get the day and month and year 3 weeks ago relative to current day.

I do not know is that possible? given that I only have to use the current day month and year to retrieve past date, and I should be able to retrieve the past date as day, month, and year to use in the script.

If my question is not clear, please let me know. I apologize.


strtotime will give you a past date in unix time, which you can then feed into the date function:

echo date('j n Y', strtotime("-3 weeks"))


Use the DateTime class instead of timestamp. With php 5.2 and above:

$date = new DateTime(); //defaults to the current date/time
echo $date->modify("-3 week")->format("Y-m-d");

This class can also handle dates before 1970 and after 2038.


you can use strtotime for that purpose:

echo date('j n Y',strtotime('-1 week'))
0

精彩评论

暂无评论...
验证码 换一张
取 消