Strtotime('-1 month') returning wrong date if month have 31 days

Asked
Active3 hr before
Viewed126 times

7 Answers

monthreturning
90%

As you can see working with the end of the month can be problematic because of how PHP works with dates. Your best bet is to go back to the beginning of the month, do your date math (i.e. go backwards in time), and then go to the date you want. That way you can check to see if the current day is greater than the number of days in month. If so, use the last day of the month instead., How does the Bladesinging wizard's Extra Attack feature interact with the additional Attack action from the Haste spell? ,The problem is in $date_month2 that represents February, the output is 2020-03-01 instead 2020-02-29 and I suppose that problem will happen in months who have 30 days when present date have 31 days., How to translate this English idiom into German: "to have something in the palm of your hand"

As you can see working with the end of the month can be problematic because of how PHP works with dates. Your best bet is to go back to the beginning of the month, do your date math (i.e. go backwards in time), and then go to the date you want. That way you can check to see if the current day is greater than the number of days in month. If so, use the last day of the month instead.

function getMonthsAgo(int $n): string {
   $date = new DateTime();
   $day = $date - > format('j');
   $date - > modify('first day of this month') - > modify('-'.$n.
      ' months');
   if ($day > $date - > format('t')) {
      $day = $date - > format('t');
   }
   $date - > setDate($date - > format('Y'), $date - > format('m'), $day);
   return $date - > format('Y-m-d');
}

// Dates Formated
$date_now = date('Y-m-d');
$date_month1 = getMonthsAgo(1);
$date_month2 = getMonthsAgo(2);
$date_month3 = getMonthsAgo(3);

//Output
echo $date_now;
echo $date_month1;
echo $date_month2;
echo $date_month3;
load more v
88%

Returns a timestamp on success, false otherwise. , date_​timestamp_​set , date_​timestamp_​get , The timestamp which is used as a base for the calculation of relative dates.

load more v
72%

(Yes I know that first it gets the 01 of current month and then adds 1 month),echo date("M-y", mktime(0, 0, 0, 12+$i, 1, date("Y")));,That behavior is perfectly expected when moving 1 month back/ahead from a 31-days month over 30-days month.

echo date('d/m/Y', strtotime('2011-05-31 first day of +1 month')); //  returns  01/06/2011
65%

< ? php
// PHP program to add days to $Date 

// Declare a date 
$date = "2019-05-10";

// Add days to date and display it 
echo date('Y-m-d', strtotime($date.
   ' + 10 days'));

?
>
load more v
75%

The strtotime function returns an integer value so it's a simple matter to see if a date is in the past or in the future.,Shown below are the results of various strings passed to strtotime and converted to date strings.,The strtotime function in PHP is incredibly powerful but very much underutilised - mainly because some aspects can be a bit confusing and people are more confident 'doing things the hard way'. The strtotime function doesn't just convert date strings to timestamp values but also has its own 'language' to let you specify just about any date or time you want.,Obviously you will need a function for this and not just a single strotime command. This should get you started:

For my server;

date('l jS F (Y-m-d)', strtotime('+$kac days'))

date('l jS F (Y-m-d)', strtotime('+$kac days'))

You need to use double quotes when the string contains a variable:
date('l jS F (Y-m-d)', strtotime("+{$kac} days"));

date('l jS F (Y-m-d)', strtotime("+{$kac} days"));

In PostgreSQL you can use:

... BETWEEN CURRENT_DATE - INTERVAL '5 years + 1 day' AND CURRENT_DATE +
INTERVAL '5 years + 1 day' ...

...BETWEEN CURRENT_DATE - INTERVAL '5 years + 1 day'
AND CURRENT_DATE +
   INTERVAL '5 years + 1 day'...

Something like this should work:

$date = date('Y-m-d', strtotime('-6 hours'))
$time = date('H:i:s');

$date = date('Y-m-d', strtotime('-6 hours'))
$time = date('H:i:s');

This might be what you're after:

$last_saturday_this_month = date('l jS F', strtotime("next month last Saturday"));

$last_saturday_this_month = date('l jS F', strtotime("next month last Saturday"));

Wondering if you could help me understand why the results of this code is off by one (1) for actual day of year?

echo date('z', strtotime('2012-5-9'));
// shows 129 and s/b 130?

echo date('z', strtotime('2012-5-9'));
// shows 129 and s/b 130?

It's really not as complicated as you think:

$dayofweek = date('l', strtotime('26 feb 2028')); // Saturday

$dayofweek = date('l', strtotime('26 feb 2028')); // Saturday

<?PHP
for($year=2000; $year <= 2050; $year++) {
echo date('l jS F, Y', strtotime("{$year}-07-01 second Saturday")) . "<br>";
}
?>

<?PHP
  for($year=2000; $year <= 2050; $year++) {
    echo date('l jS F, Y', strtotime("{$year}-07-01 second Saturday")) . "<br>";
  }
?>
load more v
40%

Method 2: Using date_add() Function: The date_add() function is used to add days, months, years, hours, minutes and seconds.Syntax:  ,Adding days to $Date can be done in many ways. It is very easy to do by using built-in functions like strtotime(), date_add() in PHP.Method 1: Using strtotime() Function: The strtotime() Function is used to convert an English textual date-time description to a UNIX timestamp. Syntax:  ,Program: PHP program to add days to $Date in PHP using strtotime() function.  ,PHP | unset() Function

Adding days to $Date can be done in many ways. It is very easy to do by using built-in functions like strtotime(), date_add() in PHP.
Method 1: Using strtotime() Function: The strtotime() Function is used to convert an English textual date-time description to a UNIX timestamp. 
Syntax: 
 

strtotime($EnglishDateTime, $time_now)
Output:
2019 - 05 - 20

date_add(object, interval);
Output:
2019 - 05 - 20
load more v
22%

strtotime('+1 month'), strtotime('-1 month') If there are 28 or 31 days in a month, there will be problems, such as strtotime('+1 month', strtotime('2019-10-31 ')) will skip to December instead of Nov...,People often get confused when strtotime is combined with -1 month, +1 month, and next month, and then they feel that this function is a bit unreliable, and there are problems at every turn. When using it, they will panic...,PHP gets the date of the previous month, next month, and this month (strtotime(), date()),The magical use of date and strtotime functions in php

date("Y-m-d", strtotime("-1 month"))
load more v

Other "month-returning" queries related to "Strtotime('-1 month') returning wrong date if month have 31 days"