0

I use

$date = date("Y-m-d");

and in sql i use between max is = date("Y-m-d") min is = 6 days back

Is there a function that gives from date back $limit?

4
  • Sorry I don't understand exactly what you want Commented Apr 17, 2012 at 17:55
  • i have date("Y-m-d") = 2012-04-12 what i want is that also 2012-04-05 is there a function that give me 6 days ago Commented Apr 17, 2012 at 17:57
  • 1
    I think that what Dan is suggesting is that you edit your question to make it more clear. For example, use an example. Commented Apr 17, 2012 at 17:59
  • Yeah try to improve the question, although I think I understood it now. But SO serves as library where everyone can look up questions/answers. Commented Apr 17, 2012 at 18:00

3 Answers 3

3

In PHP, it could be done like this:

$date_first    = date("Y-m-d"); //today's date or use some other date
$date_second = date("Y-m-d", strtotime(date("Y-m-d", strtotime($date_first)) . " -6 day")); //date before 6 days

EDIT

Based on Dan Lee's suggestion(see the comment below):

$date_before = date("Y-m-d", strtotime("-6 day"));
Sign up to request clarification or add additional context in comments.

3 Comments

strtotime('-6 days') is enough, but you would need to cover it with date()
@Dan Lee: Thanks for the suggestion. I have added that solution also(credit goes to you) :)
@Mert Metin: yeah sorry for that. I forgot to wrap it in a date() to display a formatted date, instead of the timestamp. What you are getting is the timestamp. So, just pass it to the date() function along with the date format "Y-m-d".
0

You can use MySQL only for this task.

Take DATE_SUB() to subtract from the current time:

SELECT * FROM table WHERE date BETWEEN DATE_SUB(NOW(), INTERVAL 6 DAY) AND CURDATE()

Comments

0

You could also use the OO variation of @AkhileshBChandran's answer:

$dt = new DateTime('-6 days');
$sixDaysAgo = $dt->format('Y-m-d');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.