php get date -1 month

echo strtotime("-1 day"); //1332864156
echo date("y-m-d",strtotime("-1 day")); // 12-03-27

http://us2.php.net/manual/en/function.strtotime.php


I'm surprised this answer is missing here:

$oneMonthAgo = new \DateTime('1 month ago');
echo $oneMonthAgo->format('Y-m-d');

Today is 2019-01-28. The code above outputs 2018-12-28

Here it is live to play with: http://sandbox.onlinephpfunctions.com/code/ad457f441719c6b9f68dc646445aac86a3f7f7a0


First get the date in one month with php:

$myDate = date("Y-m-d", strtotime( date( "Y-m-d", strtotime( date("Y-m-d") ) ) . "+1 month" ) );

Then you can do a select:

$result = mysql_query( 'SELECT * FROM myTable WHERE expire_date BETWEEN NOW AND "' . $myDate . '"' );

And there you have an array with all the items who expire in less than one month. If you want exactly the ones that expire in 1 month:

$result = mysql_query( 'SELECT * FROM myTable WHERE expire_date = "' . $myDate . '"' );

I hope this helps


Simply ;

    $WeekAgo = strtotime("-1 week"); //A week before today
    $MonthAgo = strtotime("-1 month"); //A month before today

Tags:

Mysql

Php