473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to convert current date to last year date?

If user input the date is 2006-07-01. How to convert it to last year
2005-07-01?

Thanks

Jul 9 '06 #1
6 9417
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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John | last post by:
If anyone can help me out with a good way to do this in javascript I would greatly appreciate it. I need to compute three dates in javascript - all of which relate to a given date, say todays...
8
by: Tim | last post by:
Does anyone know how to convert a date to the hundred year date format. For example, how would i convert 8/11/2004 to its hundred year date format. Thanks! Tim
1
by: Sam | last post by:
How do I convert Julian Date to Calendar Date in ASP.Net 1.1 based on following guideline found at Internet? To convert Julian date to Gregorian date: double JD = 2299160.5; double Z =...
3
by: yxq | last post by:
Hello, The XP Desktop clean wizard can get the last access time of desktop shortcut, i found that the info come from ...
5
by: rjfjohnson | last post by:
Hey, Today is Thursday 16-Feb-06. The same thursday last year is 17-Feb-05. Because I am comparing daily sales between years, I need to know the date of the same weekdayname as last year, so...
1
by: Shuan | last post by:
snoopy class can retrieve the specific page's lat modification date in String but is there any way to get into the integer or long? <?php include( "Snoopy.class.php" ); $request = new...
1
by: Jimmy | last post by:
I'm trying to run a query that will show only records where the year in a date field equals the current year: SELECT tblSideJob.SideJobID, tblSideJob.Date FROM tblSideJob WHERE...
3
by: Johan Mcgillicutty | last post by:
Could use some help on this one- Im new to MS Access, and even newer to SQL. Im building a database for clients of a treatment center, and need to keep track of when people are due for their...
1
by: appu29 | last post by:
In my webpage, when i click "Past or Future" button to display "previous year leave summary or future year leave summary" respectively. It gives the error "Can't use an undefined value as a HASH...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.