-2

I need to calculate the last specific day from today, I looking for something like this:

echo date('Y-m-d', strtotime("last 10"));
// if current date is '2019-02-04' outputs '2019-01-10'
// if current date is '2019-02-14' outputs '2019-02-10'

Thanks

rkeet
  • 3,406
  • 2
  • 23
  • 49
Fabio
  • 31
  • 6
  • What is the "last specific day"? Would a native speaker understand this? Accoding the example, I guess 10 days ago? – Pinke Helga Feb 24 '19 at 18:21
  • Not sure what you want to achieve, and since you tag Zend Framework 3 and then write a piece of vanilla PHP it's not quite clear if you are looking for a Zend Framework specific solution or not. In vanilla PHP you could do something like set a date to today's date, modify the day of that date to the 10:th and check if that is smaller than the current date. If so, then the "answer" is found. If it's not, then the correct answer is today's date minus one month and day set to the 10:th. – inquam Feb 24 '19 at 18:48

3 Answers3

1

Check if the "d" is 10 or more, if yes output today's date but with day 10.
Else subtract a month from the date.

if(date("d") >= 10){
    echo date('Y-m-10');
}else{
    echo date('Y-m-10', strtotime("last month"));
}
Andreas
  • 23,610
  • 6
  • 30
  • 62
0

You can calculate expressions like 'now - 10 Days'.

$days = 10
date('Y-m-d', strtotime("now - $days Days"));
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
-1

Try this


$lastday=5;
echo date('Y-m-d', strtotime("-$lastday days"));

pratik shah
  • 17
  • 1
  • 5
  • 2
    Hi, while this may be the answer, the same code with minor naming differences was already submitted. Please reframe from copying existing answers. – rkeet Feb 24 '19 at 18:41
  • "Try this" does not make for a good answer. You should explain *how* and *why* this solves their problem. I recommend reading, "[How do I write a good answer?"](http://stackoverflow.com/help/how-to-answer) – John Conde Feb 24 '19 at 18:43
  • Hi @rkeet I didn't copy. I was typing from mobile so I got late.@john Conde sure I will elaborate more – pratik shah Feb 25 '19 at 15:07