By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,663 Members | 762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,663 IT Pros & Developers. It's quick & easy.

How to convert current date to last year date?

P: n/a
If user input the date is 2006-07-01. How to convert it to last year
2005-07-01?

Thanks

Jul 9 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
phforum wrote:
If user input the date is 2006-07-01. How to convert it to last year
2005-07-01?
If this is the fixed pattern, the simplest thing you can do is something as
follows:

$date = "2006-07-01";
list($y, $m, $d) = explode('-', $date);
$newdate = (--$y) . "-$m-$d";

However, this will cause problems when the date is the 29th of February in a
leap year, while the previous is a regular year. To fix this, you can apply
the strtotime() function:

$date = "2004-02-29";
$ts = strtotime("last year", strtotime($date));
$newdate = date('Y-m-d', $ts);
HTH;
JW
Jul 9 '06 #2

P: n/a
Message-ID: <11**********************@s13g2000cwa.googlegroups .comfrom
phforum contained the following:
>If user input the date is 2006-07-01. How to convert it to last year
2005-07-01?
Lots of ways, depending on what you want to do with it. If you want to
create a timestamp, for instance, mktime() is good.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 9 '06 #3

P: n/a
On Sun, 09 Jul 2006 02:05:51 -0700, phforum wrote:
If user input the date is 2006-07-01. How to convert it to last year
2005-07-01?
The simplest way is as follows:

$date_user_entered = strtotime($_POST["date"]);
$date_one_year_ago = strtotime("-1 year", $date_user_entered);

This also works perfectly with leap years which some other solutions may
not.

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jul 9 '06 #4

P: n/a

Andy Jeffries wrote:
On Sun, 09 Jul 2006 02:05:51 -0700, phforum wrote:
If user input the date is 2006-07-01. How to convert it to last year
2005-07-01?

The simplest way is as follows:

$date_user_entered = strtotime($_POST["date"]);
$date_one_year_ago = strtotime("-1 year", $date_user_entered);

This also works perfectly with leap years which some other solutions may
not.

Cheers,
In one line:

$date_one_year_ago = strtotime("{$_POST['date']} -1 year",
$date_user_entered);

Jul 9 '06 #5

P: n/a
On Sun, 09 Jul 2006 07:50:41 -0700, Chung Leong wrote:
If user input the date is 2006-07-01. How to convert it to last year
2005-07-01?

The simplest way is as follows:

$date_user_entered = strtotime($_POST["date"]); $date_one_year_ago =
strtotime("-1 year", $date_user_entered);

This also works perfectly with leap years which some other solutions may
not.

In one line:
I was trying to keep it simple... :-)
$date_one_year_ago = strtotime("{$_POST['date']} -1 year",
$date_user_entered);
What's the $date_user_entered for in your version (which will be null
unless assigned) ;-)

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Jul 9 '06 #6

P: n/a
Thanks Andy

Simply and it's works


Andy Jeffries 寫道:
On Sun, 09 Jul 2006 02:05:51 -0700, phforum wrote:
If user input the date is 2006-07-01. How to convert it to last year
2005-07-01?

The simplest way is as follows:

$date_user_entered = strtotime($_POST["date"]);
$date_one_year_ago = strtotime("-1 year", $date_user_entered);

This also works perfectly with leap years which some other solutions may
not.

Cheers,
Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos
Jul 10 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.