473,386 Members | 1,817 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 do I do date math on 'mm/dd/yyyy'? Example: 10 years earlier than today.

I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier
than today.

How do I do that?

TIA,

Larry Woods
Jan 13 '06 #1
16 2345
On Fri, 13 Jan 2006 07:21:40 -0800, lwoods wrote:
I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier
than today.

How do I do that?

TIA,

Larry Woods

PEAR Date package can subtract/add seconds.

--
http://www.mgogala.com

Jan 13 '06 #2
Thanks, but I don't need to subtract seconds, really. I need something that
looks like this:

I need to be able to:

1. Subtract 10 years from today
2. Display that value in 'mm/dd/yyyy' format

I guess I assumed that PHP had the capability of doing this without
resorting to additional packages like PEAR

Larry

"Mladen Gogala" <go****@sbcglobal.net> wrote in message
news:pa****************************@sbcglobal.net. ..
On Fri, 13 Jan 2006 07:21:40 -0800, lwoods wrote:
I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier
than today.

How do I do that?

TIA,

Larry Woods

PEAR Date package can subtract/add seconds.

--
http://www.mgogala.com

Jan 13 '06 #3
lwoods wrote:
Thanks, but I don't need to subtract seconds, really. I need something that
looks like this:

I need to be able to:

1. Subtract 10 years from today
2. Display that value in 'mm/dd/yyyy' format


$date = "01/14/2005";
$date_new = date('m\/d\/Y', strtotime("10 years ago", strtotime($date)));
echo $date_new;

--
Andrew @ Rockface
np: Conflict - Vietnam [stopped]
www.rockface-records.co.uk
Jan 13 '06 #4
On Fri, 13 Jan 2006 07:48:34 -0800, lwoods wrote:
Thanks, but I don't need to subtract seconds, really. I need something that
looks like this:

I need to be able to:

1. Subtract 10 years from today
That only means that you have to extract enough seconds. Try with
3650*24*3600

2. Display that value in 'mm/dd/yyyy' format


And the Date class has a way of doing that.

--
http://www.mgogala.com

Jan 13 '06 #5
"lwoods" <la***@lwoods.com> kirjoitti
viestissä:ZGOxf.7893$JT.6131@fed1read06...
I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier
than today.

How do I do that?

$date = "12/31/2005";
echo date('d/m/Y', strtotime("$date -10years"));

--
SETI @ Home - Donate your cpu's idle time to science.
Further reading at <http://setiweb.ssl.berkeley.edu/>
Kimmo Laine <an*******************@gmail.com.NOSPAM.invalid>
Jan 13 '06 #6
lwoods wrote:
I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier
than today.

How do I do that?


Lots of ways.
Here's one

echo date('m/d/Y', mktime(0, 0, 0, date('m'), date('d'), date('Y')-10));

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jan 13 '06 #7
Take the current year and substract 10. It's not rocket science.

$dzisiaj = getdate();
echo sprintf("%02d/%02d/%d", $dzisiaj['mon'], $dzisiaj['mday'],
$dzisiaj['year'] - 10);

Jan 13 '06 #8
lwoods wrote:
I want to calculate and display the 'mm/dd/yyyy' that is 10 years earlier
than today.

How do I do that?

TIA,

Larry Woods


explode() it, perform the calculation, and then implode() it. That's
what I would do, anyhow.

// configuration
$date = '01/01/2006';
$offset = '-10';
$date_glue = '/';

// implementation
$temp = explode($date_glue, $date);
$temp[2] = $temp[2] + $offset;
$date = implode ($date_glue, $date);
echo $date;
Jan 13 '06 #9
VS
jo*********@sbcglobal.net wrote:
lwoods wrote:
I want to calculate and display the 'mm/dd/yyyy' that is 10 years
earlier than today.

How do I do that?

TIA,

Larry Woods

explode() it, perform the calculation, and then implode() it. That's
what I would do, anyhow.

// configuration
$date = '01/01/2006';
$offset = '-10';
$date_glue = '/';

// implementation
$temp = explode($date_glue, $date);
$temp[2] = $temp[2] + $offset;
$date = implode ($date_glue, $date);
echo $date;


That technique only works for some date calculations.

Try to take off 10 days during the first 10 days of the month or 10
months between Jan and October and you'll get strange results.

--
VS
Jan 13 '06 #10
VS wrote:
jo*********@sbcglobal.net wrote:
lwoods wrote:
I want to calculate and display the 'mm/dd/yyyy' that is 10 years
earlier than today.

How do I do that?

TIA,

Larry Woods


explode() it, perform the calculation, and then implode() it. That's
what I would do, anyhow.

// configuration
$date = '01/01/2006';
$offset = '-10';
$date_glue = '/';

// implementation
$temp = explode($date_glue, $date);
$temp[2] = $temp[2] + $offset;
$date = implode ($date_glue, $date);
echo $date;

That technique only works for some date calculations.

Try to take off 10 days during the first 10 days of the month or 10
months between Jan and October and you'll get strange results.


Well, sure, but he was only asking about the year hehe. For the day or
month, a more advanced version would be required.

John D. Mann
Jan 13 '06 #11
On Fri, 13 Jan 2006 15:26:11 GMT, Mladen Gogala <go****@sbcglobal.net>
wrote:

That only means that you have to extract enough seconds. Try with
3650*24*3600


Really?
What about leap-years?


--
Exact Meta Search | Major Search Engine http://exactsearcher.com
Web Design Essex | Multimedia | Printing http://nextwave.co.uk
Jan 13 '06 #12
jo*********@sbcglobal.net said the following on 13/01/2006 18:34:
VS wrote:
jo*********@sbcglobal.net wrote:
lwoods wrote:

I want to calculate and display the 'mm/dd/yyyy' that is 10 years
earlier than today.

// configuration
$date = '01/01/2006';
$offset = '-10';
$date_glue = '/';

// implementation
$temp = explode($date_glue, $date);
$temp[2] = $temp[2] + $offset;
$date = implode ($date_glue, $date);
echo $date;


That technique only works for some date calculations.

Try to take off 10 days during the first 10 days of the month or 10
months between Jan and October and you'll get strange results.


Well, sure, but he was only asking about the year hehe. For the day or
month, a more advanced version would be required.


How about 29/02/2004?
--
Oli
Jan 14 '06 #13
Berimor wrote:
On Fri, 13 Jan 2006 15:26:11 GMT, Mladen Gogala <go****@sbcglobal.net>
wrote:

That only means that you have to extract enough seconds. Try with
3650*24*3600


Really?
What about leap-years?


And wasn't there a leap second at the beginning of this month?

--
philronan [@] blueyonder [dot] co [dot] uk

Jan 15 '06 #14
Oli Filth wrote:
jo*********@sbcglobal.net said the following on 13/01/2006 18:34:
VS wrote:
jo*********@sbcglobal.net wrote:

lwoods wrote:

> I want to calculate and display the 'mm/dd/yyyy' that is 10 years
> earlier than today.
>
// configuration
$date = '01/01/2006';
$offset = '-10';
$date_glue = '/';

// implementation
$temp = explode($date_glue, $date);
$temp[2] = $temp[2] + $offset;
$date = implode ($date_glue, $date);
echo $date;


That technique only works for some date calculations.

Try to take off 10 days during the first 10 days of the month or 10
months between Jan and October and you'll get strange results.


Well, sure, but he was only asking about the year hehe. For the day
or month, a more advanced version would be required.


How about 29/02/2004?


hehe - Well, the title of the thread says mm/dd/yyyy. So, again, i was
just going by the data given by the thread starter. You can always
adapt your code to handle the format of dd/mm/yyyy. Something like this...

// configuration
$date = '01/01/2006';
$offset = '-10';
$date_glue = '/';

// change if your month is in a different position, such as dd/mm/yy
$month_location = '1';
$day_location = '2';

// implementation
$temp = explode($date_glue, $date);
if $temp[$day_location - 1] < 11
{
$eat_previous_month_by_this = 10 - $temp[$day_location - 1];
switch ($temp[$month_location - 1]) {
case 1:
$temp[$month_location - 1] = 12;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 2:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 3:
$temp[$month_location - 1]--;
// check for leap year here. is year divisible by four? if so, then
use 29, if not then use 28. a simple if statement should suffice.
$temp[$day_location - 1] = 28 - $eat_previous_month_by_this;
break;
case 4:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 5:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 30 - $eat_previous_month_by_this;
break;
case 6:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 7:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 30 - $eat_previous_month_by_this;
break;
case 8:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 9:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 10:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 30 - $eat_previous_month_by_this;
break;
case 11:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 12:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 30 - $eat_previous_month_by_this;
break;
}
}
$temp[$day_location - 1] = $temp[$day_location - 1] + $offset;
$date = implode ($date_glue, $date);
echo $date;
Jan 15 '06 #15
jo*********@sbcglobal.net said the following on 15/01/2006 13:11:
Oli Filth wrote:
jo*********@sbcglobal.net said the following on 13/01/2006 18:34:
VS wrote:

jo*********@sbcglobal.net wrote:

> lwoods wrote:
>
>> I want to calculate and display the 'mm/dd/yyyy' that is 10 years
>> earlier than today.
>>
> // configuration
> $date = '01/01/2006';
> $offset = '-10';
> $date_glue = '/';
>
> // implementation
> $temp = explode($date_glue, $date);
> $temp[2] = $temp[2] + $offset;
> $date = implode ($date_glue, $date);
> echo $date;


That technique only works for some date calculations.

Try to take off 10 days during the first 10 days of the month or 10
months between Jan and October and you'll get strange results.
Well, sure, but he was only asking about the year hehe. For the day
or month, a more advanced version would be required.


How about 29/02/2004?


hehe - Well, the title of the thread says mm/dd/yyyy. So, again, i was
just going by the data given by the thread starter. You can always
adapt your code to handle the format of dd/mm/yyyy. Something like this...


Oops, my mistake, I meant 02/29/2004 then... i.e. a day that only exists
in leap years.

--
Oli
Jan 15 '06 #16
Philip Ronan wrote:
And wasn't there a leap second at the beginning of this month?


Nope. It was at the end of last month.

23:59:59 was followed by 23:59:60
<http://www.timeanddate.com/time/leapseconds.html>

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jan 15 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
4
by: Lynn | last post by:
On a form I have Date_Start Date_End I have a new Date_Start1 Date_End1 which the use inputs. I need to validate that Date_Start1 and...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
4
by: peashoe | last post by:
I have an asp page that uses a calendar.js (pop-up) file to add an exact date format in the text field (txtDDate). My problem is I need some javascript that sets an alert that does not allow them...
6
by: Jim Davis | last post by:
Before I reinvent the wheel I thought I'd ask: anybody got a code snippet that will convert the common ISO8601 date formats to a JS date? By "common" I mean at the least ones described in this...
12
by: Woody Splawn | last post by:
I am trying to determine the age of a person based on two dates, the Date of Birth and Today(). I have a function that does this but it is kludgey and is giving me an age that is pretty close...
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
0
by: kirby.urner | last post by:
Cyber-curricula have a leveling aspect, as kids nearer Katrina's epicenter tune in and bliss out on 'Warriors of the Net' (why wait for stupid big dummy textbooks to catch up?). They feel more...
4
by: jmarcrum | last post by:
Hi everyone!! I have a continuous form that allows users to select a record and chnage the DATE that the record is assigned to another DATE within the same year. The button is called "Change plan...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.