Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php get date -1 month

Tags:

php

mysql

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.

like image 825
gogu Avatar asked Mar 28 '12 15:03

gogu


People also ask

How to retrieve current month in PHP?

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.

How do I get the current date and time in PHP?

(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.

What is the PHP date () function?

The PHP Date () Function The PHP date () function formats a timestamp to a more readable date and time.

How to increment date by one month in PHP?

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.


3 Answers

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

like image 149
Expert wanna be Avatar answered Oct 20 '22 02:10

Expert wanna be


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

like image 23
Tales Avatar answered Oct 20 '22 01:10

Tales


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

like image 14
John Linhart Avatar answered Oct 20 '22 03:10

John Linhart