V
V
vetsinen2018-04-10 12:46:35
PHP
vetsinen, 2018-04-10 12:46:35

Strange behavior of strtotime in php, can it be accepted or fixed?

Good afternoon, I'm trying to get the same date in php, a month ago. the following code gives a rather strange result

$d = strtotime("2018-03-31 -1 month");
echo gmdate("Y-m-d", $d); // result: 2018-03-03

that is, PHP believes that if March 31 is scrolled back a month, we will get March 3. should I accept this as normal or look for another function, or even write my own? Are there any libraries/implementations that give a more intuitive date shift result?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2018-04-10
@Rsa97

It's just the vagueness of the concept of "a month ago". Try to write down what date was a month ago for:
- February 27
- February 28
- March 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question