Date Arithmetic  
Author Message
Kurt Harless





PostPosted: 2004-8-30 23:26:00 Top

java-programmer, Date Arithmetic Arg,

So I know I use the Calendar abstract class. And I am betting that I use
the Add method.
Could I see a example of some date arithmetic, e.g. number of days from
today since 01/01/1964.

Must I work out the differences by year, month, day individually, convert to
a common unit of time, add them, then convert to a displayable unit of time?

Struggling...


 
Paul Lutus





PostPosted: 2004-8-31 14:01:00 Top

java-programmer >> Date Arithmetic Kurt Harless wrote:

> Arg,
>
> So I know I use the Calendar abstract class. And I am betting that I use
> the Add method.
> Could I see a example of some date arithmetic, e.g. number of days from
> today since 01/01/1964.
>
> Must I work out the differences by year, month, day individually, convert
> to a common unit of time, add them, then convert to a displayable unit of
> time?

Practice with the GregorianCalendar class and its many online tutirials.

--
Paul Lutus
http://www.arachnoid.com

 
Jacob





PostPosted: 2004-8-31 19:46:00 Top

java-programmer >> Date Arithmetic Kurt Harless wrote:

> So I know I use the Calendar abstract class. And I am betting that I use
> the Add method.
> Could I see a example of some date arithmetic, e.g. number of days from
> today since 01/01/1964.
>
> Must I work out the differences by year, month, day individually, convert to
> a common unit of time, add them, then convert to a displayable unit of time?

Look at

http://geosoft.no/software/day/Day.java.html

It is a simple API for doing date arithmetics
and finding number of days between dates among
others.

 
 
wesley





PostPosted: 2006-12-5 22:10:00 Top

java-programmer >> Date Arithmetic Hi

I have searched through the archives but did not find a solution to
this. Is there a way to get the last day of the month. In other words,
can you determine the last date of the current month without having to
check the Month and leap year to confirm?

Is there also a way to determine the date, one month from now without
actually checking the date and the Leap year?

Thanks
Wes

 
 
larry





PostPosted: 2006-12-5 22:41:00 Top

java-programmer >> Date Arithmetic
email***@***.com wrote:
> Hi
>
> I have searched through the archives but did not find a solution to
> this. Is there a way to get the last day of the month. In other words,
> can you determine the last date of the current month without having to
> check the Month and leap year to confirm?
>
> Is there also a way to determine the date, one month from now without
> actually checking the date and the Leap year?
>
> Thanks
> Wes

$mn = date("m");
$yr = date("Y");

$testdate = mktime(0,0,0,$mn,1,$yr); //first date in selected month
as unix timestamp
$firstday = date(w,$testdate); // what day does month start on?
$numdays = date(t,$testdate); // number of days in this month
$daystop= mktime(0,0,0,$mn,$numdays,$yr); // what day is the last
day??

Larry

 
 
Michael Fesser





PostPosted: 2006-12-5 22:41:00 Top

java-programmer >> Date Arithmetic .oO(email***@***.com)

>I have searched through the archives but did not find a solution to
>this. Is there a way to get the last day of the month. In other words,
>can you determine the last date of the current month without having to
>check the Month and leap year to confirm?
>
>Is there also a way to determine the date, one month from now without
>actually checking the date and the Leap year?

Try <http://www.php.net/strtotime>.

Micha
 
 
Steve





PostPosted: 2006-12-6 0:08:00 Top

java-programmer >> Date Arithmetic
<email***@***.com> wrote in message
news:email***@***.com...
| Hi
|
| I have searched through the archives but did not find a solution to
| this. Is there a way to get the last day of the month. In other words,
| can you determine the last date of the current month without having to
| check the Month and leap year to confirm?
|
| Is there also a way to determine the date, one month from now without
| actually checking the date and the Leap year?
|

$lastOfMonth = date(
'm/d/Y',
strtotime(
'-1 second',
strtotime(
'+1 month',
strtotime(
date('m') .
'/01/' .
date('Y') .
' 00:00:00'
)
)
)
);
echo '<pre>' . $lastOfMonth . '</pre>';


 
 
NC





PostPosted: 2006-12-6 5:47:00 Top

java-programmer >> Date Arithmetic email***@***.com wrote:
>
> I have searched through the archives but did not find a solution
> to this. Is there a way to get the last day of the month.

Yes:

$days = date('t');

This will return the number of days in the current month.

> Is there also a way to determine the date, one month from now
> without actually checking the date and the Leap year?

Yes:

$oneMonthFromNow = strtotime('+ 1 month');

But make sure if this returns what you expect it to; "one month"
starting on October 31, for example, would end on December 1...

Cheers,
NC

 
 
larry





PostPosted: 2006-12-6 12:30:00 Top

java-programmer >> Date Arithmetic Of course I thought it over and came up with better examples:

$mn = date("m");
$yr = date("Y");
$dy = date("d");

$numdays = date("t",mktime(0,0,0,$mn,1,$yr));
$nextmonth1 = date("m/d/Y",mktime(0,0,0,$mn,$dy+30,$yr));
$nextmonth2 = date("m/d/Y",mktime(0,0,0,$mn+1,$dy,$yr));

Mktime can take 0, negative and positive vlaues beyond the day and
month limits and interpret them; if you put $dy+240 it will calculate
the date 240 days in advance of the date, etc.

Check on the parameters of the date() function there is some goodies in
there on what it can output.

Larry

 
 
wesley





PostPosted: 2006-12-6 17:16:00 Top

java-programmer >> Date Arithmetic Hi I have tried to use some of the functions you guys suggested. Some
work fine but others like strtotime return results that i dont think
are correct: This is what i have tried:

echo strtotime('+ 1 month');
The result is 1168074894

I have also found another function to modify the date:

$date = new DateTime("2006-12-12");
$date->modify("+1 day");
echo $date->format("Y-m-d");

But this returns nothing. "DateTime" is not being recorgnized. I have
php version 5.0.4. This function is supposed to be supported. Any ideas
as to why this does not work.

Thank you for all the responses
Wes

 
 
Steve





PostPosted: 2006-12-6 23:47:00 Top

java-programmer >> Date Arithmetic
<email***@***.com> wrote in message
news:email***@***.com...
| Hi I have tried to use some of the functions you guys suggested. Some
| work fine but others like strtotime return results that i dont think
| are correct: This is what i have tried:
|
| echo strtotime('+ 1 month');
| The result is 1168074894

the result is the number of seconds since a beginning date (i believe
01-01-1970, iirc). you can see the results as i assume you expect, doing
this:

echo date('m-d-Y', strtotime('+1 month'));


 
 
NC





PostPosted: 2006-12-7 11:39:00 Top

java-programmer >> Date Arithmetic email***@***.com wrote:
>
> I have tried to use some of the functions you guys suggested.
> Some work fine but others like strtotime return results that i dont
> think are correct: This is what i have tried:
>
> echo strtotime('+ 1 month');
> The result is 1168074894

And why is it not correct? 1168074894 is the Unix time stamp for
some time on January 6, 2007... Have you read the PHP
documentation at all? If you did, you'd know that strtotime() returns
a timestamp and you will need to call date() to format a date
represented by that timestamp, like so:

echo date('Y-m-d', strtotime('+ 1 month'));

For more information, see:

http://www.php.net/strtotime
http://www.php.net/date

> I have also found another function to modify the date:
>
> $date = new DateTime("2006-12-12");
> $date->modify("+1 day");
> echo $date->format("Y-m-d");
>
> But this returns nothing. "DateTime" is not being recorgnized.

But of course it isn't. You try to refer to a class called DateTime;
as far as I can tell, this is not something that ships with PHP.
So you need to find out where this class came from and include()
the appropriate files...

> This function is supposed to be supported.

I don't think so. Not to mention the fact this is not a function,
but a class...

Cheers,
NC