1 Year adding in date

Hi. I wanted to know the simplest way to show reminder after one year of today’s date. Like if I purchased a product on 12/12/2011, the reminder will be shown on 12/12/2011. Similarly how can I add 18 days in today’s date?Can anyone help me out in this regard?


echo date('d/m/Y', strtotime('+18 day'));

To add one year to a date you could do something like:


$date = '05/06/2011';
$date = strtotime($date);
$new_date = strtotime('+ 1 year', $date);
echo date('d/m/Y', $new_date);

If the date is kept in a mysql database, as say, purchase_date:


purchases
========
id | 23
name | "Nick Clegg" 
purchase_date | "2010-12-12"
title | "Supporting the Euro"
// etc

find purchases from exactly a year ago today:


"Select id, name from purchases where purchase_date = DATE_SUB(CURRENT_DATE(), INTERVAL 1 YEAR)";

This is untested, and there are other ways to acheive the same, take a look at the Mysql manual page on date manipulation.

@Immerse – certainly in the UK (or is it europe?), this strtotime() behaviour always worries me, how does it know you mean 5th June and not 6th May in your input?


$date = '05/06/2011'; 
$date = strtotime($date); 
$new_date = strtotime('+ 1 year', $date); 
echo date('d/m/Y', $new_date);

Gives 06/05/2012

The last line suggests you meant 5th June, but what you get back is 6th May.

This is no doubt true of the whole DateTime class too, which IIRC is built upon strtotime().

EDIT

Its not the fault of strtotime, btw, but is something we have to watch out for.

To be honest, I generally use strtotime() on dates from the database, which works OK (Y-m-d format).
But for European/ UK dates, good point :wink:

Thx a lot Immerse and Cups.I will go through both the methods and will let u know. :slight_smile:

I have got desired result using both the methods.Thanks for ur valuable replies.

This worked like magic for me:

$new_date = strtotime('+ 18 day', $date);
echo date('d/m/Y', $new_date);

My solution was designed to help you understand how you might select matching dates from your mysql database.

Anyhow, glad you found out how to do what you wanted.

well how can i design my query to find out the reminders which are coming only after current date? like “where purchase date > current date”??

“Select id, name from purchases where purchase_date = CURRENT_DATE()”;

In addition:
If your date filed is ‘datetime’ instead of only type of ‘date’ then you must format the date for the field:


SELECT id, name FROM purchases WHERE DATE_FORMAT(purchase_date, '%Y-%m-%d')=CURRENT_DATE;

Otherwise you will not get desired results!

ok.got it.never thought it can be so easy :smiley:

Hi again.Though the matter is solved, but it cant display the desired result in reminder section :confused:
Is this possible to do this through query?

WHERE  sales.inv_date = CURRENT_DATE - 18 days 

And

WHERE  sales.inv_date = CURRENT_DATE - 1 year 

This way when the date after 1 year or 18 days will come, it will display today’s data.

Look at my previous reply at #3 above :wink:

That worked well. :slight_smile:
Just curious to know is this syntax correct?

sales.inv_date = DATE_SUB(CURRENT_DATE(), INTERVAL 18 DAY)

Yes - unless you want to shorten CURRENT_DATE() to CURDATE() which is a shorter alternative way of referencing the current date.

if it’s short you want, change this –

sales.inv_date = DATE_SUB(CURRENT_DATE(), INTERVAL 18 DAY)

to this –

sales.inv_date = CURRENT_DATE - INTERVAL 18 DAY

i don’t like using CURDATE() in place of CURRENT_DATE because the latter is standard SQL

Thanks a lot.It was very helpful.