So here is the situation. In my db table i have a column called expire_date, where i put a certain date for each row. So for example the expire date is 28-04-2012
I need a php code that will send a certain user a notice via email that the certain thing that expires on that date is 1 month from expiring. So if today is 28-03-2012, that means that there is only 1 moonth before that thing expires. I didn't make any code for this because i don't know how to solve the -1 month.
Method 1: Using date () function to retrieve current month. PHP’s date () function can let you know date and time related information based on the formatting characters it takes in its first parameter. The function can take maximum of two parameters.
(PHP 4, PHP 5, PHP 7) getdate — Get date/time information. getdate ([ int $timestamp = time() ] ) : array. Returns an associative array containing the date information of the timestamp, or the current local time if no timestamp is given.
The PHP Date () Function The PHP date () function formats a timestamp to a more readable date and time.
Write a PHP script to increment date by one month. <?php $dt = strtotime ("2012-12-21"); echo date ("Y-m-d", strtotime ("+1 month", $dt))." "; ?> Have another way to solve this solution? Contribute your code (and comments) through Disqus. Previous: Write a PHP script to get the current month and previous three months.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With