RE: [PHP] newbie: help with date arithmetic[Scanned]

2002-11-12 Thread ROBERT MCPEAK

This is a great help.  Thanks ya'll.  And I will continue to, and do regulary RTFM 
8-)  I find that it generally sucks for a newbie.


>>> "Michael Egan" <[EMAIL PROTECTED]> 11/12/02 10:42AM >>>
Robert,

I've been looking at this myself over the past couple of days.

I gather the best approach is to convert your dates into UNIX timestamps. 

For example:

$first_unix_time = mktime($hour1, $minutes1, $seconds1, $month1, $day1, $year1);

$second_unix_time = mktime($hour2, $minutes2, $seconds2, $month2, $day2, $year2);

Subtract the one from the other to give the difference:

$difference = $first_unix_time - $second_unix_time;

The result will be in seconds so you'll need to convert this depending on the format 
you require. 

For example, to convert the difference to years you might do:

$years = floor($difference / (365 * 24 * 60 * 60));

Hope this helps,

Michael Egan

-Original Message-
From: ROBERT MCPEAK [mailto:RMCPEAK@;jhuccp.org] 
Sent: 12 November 2002 15:31
To: [EMAIL PROTECTED] 
Subject: [PHP] newbie: help with date arithmetic[Scanned]


I'm trying to add/subract two dates.  I think I need to use mktime() but I can't quite 
figure out how.

I'd like to do something like this:

(2002-11-15)-(2002-11-10)=5

or

(2002-12-10)-(2002-11-10)=20

Obviously taking into account number of days in a given month.


Does somebody have some code handy that does this?  Any help would be greatly 
appreciated!

Thanks.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php 



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] newbie: help with date arithmetic[Scanned]

2002-11-12 Thread Michael Egan
Robert,

I've been looking at this myself over the past couple of days.

I gather the best approach is to convert your dates into UNIX timestamps. 

For example:

$first_unix_time = mktime($hour1, $minutes1, $seconds1, $month1, $day1, $year1);

$second_unix_time = mktime($hour2, $minutes2, $seconds2, $month2, $day2, $year2);

Subtract the one from the other to give the difference:

$difference = $first_unix_time - $second_unix_time;

The result will be in seconds so you'll need to convert this depending on the format 
you require. 

For example, to convert the difference to years you might do:

$years = floor($difference / (365 * 24 * 60 * 60));

Hope this helps,

Michael Egan

-Original Message-
From: ROBERT MCPEAK [mailto:RMCPEAK@;jhuccp.org]
Sent: 12 November 2002 15:31
To: [EMAIL PROTECTED]
Subject: [PHP] newbie: help with date arithmetic[Scanned]


I'm trying to add/subract two dates.  I think I need to use mktime() but I can't quite 
figure out how.

I'd like to do something like this:

(2002-11-15)-(2002-11-10)=5

or

(2002-12-10)-(2002-11-10)=20

Obviously taking into account number of days in a given month.


Does somebody have some code handy that does this?  Any help would be greatly 
appreciated!

Thanks.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] newbie: help with date arithmetic

2002-11-12 Thread Ernest E Vogelsinger
At 16:30 12.11.2002, ROBERT MCPEAK spoke out and said:
[snip]
>I'm trying to add/subract two dates.  I think I need to use mktime() but I 
>can't quite figure out how.
>
>I'd like to do something like this:
>
>(2002-11-15)-(2002-11-10)=5
>
>or
>
>(2002-12-10)-(2002-11-10)=20
>
>Obviously taking into account number of days in a given month.
[snip] 

:)) rtfm, again :))

1) http://www.php.net/manual/en/function.strtotime.php
2) http://www.gnu.org/manual/tar-1.12/html_chapter/tar_7.html

3) your second equation would return 30, not 20 ;->
4) try this:

/* untested */
function days($str_from, $str_to) {
$from = strtotime(isset($str_from) ? $str_from : 'now');
$to   = strtotime(isset($str_to) ? $str_to : 'now');
if ($from > $to) {
   $x = $from; $from = $to; $to = $x;
}
$rslt = $to - $from; // number in seconds
return (int)($rslt / 86400); // return days (1d = 86400 secs)
}

Hope this helps,

-- 
   >O Ernest E. Vogelsinger 
   (\) ICQ #13394035 
^ http://www.vogelsinger.at/



[PHP] newbie: help with date arithmetic

2002-11-12 Thread ROBERT MCPEAK
I'm trying to add/subract two dates.  I think I need to use mktime() but I can't quite 
figure out how.

I'd like to do something like this:

(2002-11-15)-(2002-11-10)=5

or

(2002-12-10)-(2002-11-10)=20

Obviously taking into account number of days in a given month.


Does somebody have some code handy that does this?  Any help would be greatly 
appreciated!

Thanks.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php