Re: [PHP] Calendar/Date

2009-03-18 Thread Robert Cummings
On Tue, 2009-03-17 at 20:52 -0700, Jason Todd Slack-Moehrle wrote:
 Hi All,
 
 Does anyone have code and/or advice for how to get get the current  
 week (with a passed current day, say) and what then end date is at  
 Saturday.
 
 So take today: Tuesday March 17, 2009
 
 I want to get:
 Sunday March 15, 2009
 Monday March 16, 2009
 Tuesday March 17, 2009
 Wednesday March 18, 2009
 Thursday March 19, 2009
 Friday March 20, 2009
 Saturday March 21, 2009

A sprinkle of math + a sprinkle of time + a sprinkle of PHP:

?php

$dates = array();
$offset = (int)date( 'w' ) - 6;
for( $i = 0; $i  7; $i++ )
{
$dates[] = date( 'l F j, Y', strtotime( '+'.($offset + $i).'
days' ) );
}
 
print_r( $dates );

?

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



RE: [PHP] Calendar/Date

2009-03-18 Thread Bob McConnell
From: Paul M Foster
 On Tue, Mar 17, 2009 at 08:52:11PM -0700, Jason Todd Slack-Moehrle
wrote:
 
 Hi All,

 Does anyone have code and/or advice for how to get get the current
 week (with a passed current day, say) and what then end date is at
 Saturday.

 So take today: Tuesday March 17, 2009

 I want to get:
 Sunday March 15, 2009
 Monday March 16, 2009
 Tuesday March 17, 2009
 Wednesday March 18, 2009
 Thursday March 19, 2009
 Friday March 20, 2009
 Saturday March 21, 2009
 
 I just answered a question similar to this. You might check the
 archives. In this case, you'll need to use the getdate() function (see
 php.net/manual/en/ for details) to get the array of values for today
 (like the day of the month, month number, year, etc.). The getdate()
 function returns an array, one of whose members is 'wday', which is
the
 day of the week, starting with 0 for Sunday. Use that number to
 determine how many days to go back from today. Then use mktime() to
get
 the timestamps for each day in turn. You feed mktime() values from the
 getdate() call. Then you can use strftime() or something else to print
 out the dates in whatever format, given the timestamps you got.
 
 Be careful in feeding values to mktime(). If your week spans a
 month or year boundary, you'll need to compensate for it when giving
 mktime() month numbers, day numbers and year numbers.

You also need to be aware that on 32 bit Unix and Linux systems the
behavior of mktime() on dates after Jan 18, 2038 is undefined. The 32
bit counter overflows early on the 19th, so any value returned is
invalid. This is not a problem on 64 bit systems.

We ran into this recently because Support was defining never expire as
Today plus 30 years. A couple of sites started reporting problems about
two months ago.

Bob McConnell

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



RE: [PHP] Calendar/Date

2009-03-18 Thread Robert Cummings
On Wed, 2009-03-18 at 11:46 -0400, Bob McConnell wrote:
 From: Paul M Foster
  On Tue, Mar 17, 2009 at 08:52:11PM -0700, Jason Todd Slack-Moehrle
 wrote:
  
  Hi All,
 
  Does anyone have code and/or advice for how to get get the current
  week (with a passed current day, say) and what then end date is at
  Saturday.
 
  So take today: Tuesday March 17, 2009
 
  I want to get:
  Sunday March 15, 2009
  Monday March 16, 2009
  Tuesday March 17, 2009
  Wednesday March 18, 2009
  Thursday March 19, 2009
  Friday March 20, 2009
  Saturday March 21, 2009
  
  I just answered a question similar to this. You might check the
  archives. In this case, you'll need to use the getdate() function (see
  php.net/manual/en/ for details) to get the array of values for today
  (like the day of the month, month number, year, etc.). The getdate()
  function returns an array, one of whose members is 'wday', which is
 the
  day of the week, starting with 0 for Sunday. Use that number to
  determine how many days to go back from today. Then use mktime() to
 get
  the timestamps for each day in turn. You feed mktime() values from the
  getdate() call. Then you can use strftime() or something else to print
  out the dates in whatever format, given the timestamps you got.
  
  Be careful in feeding values to mktime(). If your week spans a
  month or year boundary, you'll need to compensate for it when giving
  mktime() month numbers, day numbers and year numbers.
 
 You also need to be aware that on 32 bit Unix and Linux systems the
 behavior of mktime() on dates after Jan 18, 2038 is undefined. The 32
 bit counter overflows early on the 19th, so any value returned is
 invalid. This is not a problem on 64 bit systems.
 
 We ran into this recently because Support was defining never expire as
 Today plus 30 years. A couple of sites started reporting problems about
 two months ago.

But the solution doesn't even require mktime().

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



Re: [PHP] Calendar/Date

2009-03-18 Thread Paul M Foster
On Wed, Mar 18, 2009 at 11:46:31AM -0400, Bob McConnell wrote:

 From: Paul M Foster

snip

 
 You also need to be aware that on 32 bit Unix and Linux systems the
 behavior of mktime() on dates after Jan 18, 2038 is undefined. The 32
 bit counter overflows early on the 19th, so any value returned is
 invalid. This is not a problem on 64 bit systems.
 
 We ran into this recently because Support was defining never expire as
 Today plus 30 years. A couple of sites started reporting problems about
 two months ago.

This is why I normally never use the time functions in PHP. Instead, I
wrote a date class that uses Julian days internally and doesn't consult
PHP time functions. When I need some odd thing (like the date for the
end of the week), I just add it as a member to the date class. Plus,
PHP's date objects are woefully unfeatureful. If someone asks on the
list for a solution, I can use the PHP time functions for advising them,
but I don't personally use them.

Paul

-- 
Paul M. Foster

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



RE: [PHP] Calendar/Date

2009-03-18 Thread Bob McConnell
From: Paul M Foster
 On Wed, Mar 18, 2009 at 11:46:31AM -0400, Bob McConnell wrote:
 
 You also need to be aware that on 32 bit Unix and Linux systems the
 behavior of mktime() on dates after Jan 18, 2038 is undefined. The 32
 bit counter overflows early on the 19th, so any value returned is
 invalid. This is not a problem on 64 bit systems.
 
 We ran into this recently because Support was defining never expire
as
 Today plus 30 years. A couple of sites started reporting problems
about
 two months ago.
 
 This is why I normally never use the time functions in PHP. Instead, I
 wrote a date class that uses Julian days internally and doesn't
consult
 PHP time functions. When I need some odd thing (like the date for the
 end of the week), I just add it as a member to the date class. Plus,
 PHP's date objects are woefully unfeatureful. If someone asks on the
 list for a solution, I can use the PHP time functions for advising
them,
 but I don't personally use them.

Unfortunately, this code was initially inherited from another project
and already had mktime() based date calculations throughout (in 19 of
162 files). I'm the fourth programmer to work with this project and now
have to correct these problems. There are several other programmers who
have to correct it in their projects as well. None of us wrote the
original code, so we are all in the same boat. No matter what we want,
we can't just replace code wholesale because of the testing overhead and
other task priorities. We can only fix it after it becomes recognized as
a problem.

The other issue is that I don't do OOP. After 30 years of writing
procedural code, mostly assembler, PL/M and C, I simply don't see the
point of OO, nor can I justify the additional overhead. I have written
functions that others converted into methods, and occasionally re-use
functions that were written as methods, but have never used a whole
class.

Bob McConnell

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



Re: [PHP] Calendar/Date

2009-03-18 Thread Paul M Foster
On Wed, Mar 18, 2009 at 12:57:39PM -0400, Bob McConnell wrote:

 From: Paul M Foster
  On Wed, Mar 18, 2009 at 11:46:31AM -0400, Bob McConnell wrote:
  
  You also need to be aware that on 32 bit Unix and Linux systems the
  behavior of mktime() on dates after Jan 18, 2038 is undefined. The 32
  bit counter overflows early on the 19th, so any value returned is
  invalid. This is not a problem on 64 bit systems.
  
  We ran into this recently because Support was defining never expire
 as
  Today plus 30 years. A couple of sites started reporting problems
 about
  two months ago.
  
  This is why I normally never use the time functions in PHP. Instead, I
  wrote a date class that uses Julian days internally and doesn't
 consult
  PHP time functions. When I need some odd thing (like the date for the
  end of the week), I just add it as a member to the date class. Plus,
  PHP's date objects are woefully unfeatureful. If someone asks on the
  list for a solution, I can use the PHP time functions for advising
 them,
  but I don't personally use them.
 
 Unfortunately, this code was initially inherited from another project
 and already had mktime() based date calculations throughout (in 19 of
 162 files). I'm the fourth programmer to work with this project and now
 have to correct these problems. There are several other programmers who
 have to correct it in their projects as well. None of us wrote the
 original code, so we are all in the same boat. No matter what we want,
 we can't just replace code wholesale because of the testing overhead and
 other task priorities. We can only fix it after it becomes recognized as
 a problem.

I feel your pain. Been there, done that. T-shirt in drawer.

 
 The other issue is that I don't do OOP. After 30 years of writing
 procedural code, mostly assembler, PL/M and C, I simply don't see the
 point of OO, nor can I justify the additional overhead. I have written
 functions that others converted into methods, and occasionally re-use
 functions that were written as methods, but have never used a whole
 class.
 

You know, I'm gonna start a programmer blog to talk about things like
this (as soon as I finish the blog software, in progress now). I'm
sympathetic. I also come from a procedural background, and frankly don't
see that the OO hype has lived up to its promise.

I see two reasons to use OO under select circumstances: First,
namespaces. The more routines you have in a given program, the more
likely you are to have function name collisions. OO helps with that. The
other thing that comes to mind is that objects can have embedded value
members which get shared between the various class methods. Meaning I
don't have the overhead and hassle of having to pass those values around
to all the functions involved. I don't know that objects have any
noticeable overhead versus functions. But I'm pretty judicious about the
use of objects-- only if that's the best solution for a given problem
(best in *my* opinion).

Paul

-- 
Paul M. Foster

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



Re: [PHP] Calendar/Date

2009-03-18 Thread Hans Åhlin
Weeks in a year is 52 or 53
Days in a year is 365 and if a leap year 366

If (

(365 (Days Of a year) * (Years From 1940)) + (Number of leap years
since 1940, (Years From 1940 / 4)  (if its a Leap year -1)) + ((Days
From 01-01) + 1) % 7 = (0-6 where 0 = Monday)

) is less then 3 (Thursday) then its in week nr 01 else if equal to 4
(Friday) its week 53 else its week 52

http://threesides.kronan-net.com/2008/08/04/date-calculation-algorithm/
-- 
MvH / Hans Åhlin - www.kronan-net.com

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



[PHP] Calendar/Date

2009-03-17 Thread Jason Todd Slack-Moehrle

Hi All,

Does anyone have code and/or advice for how to get get the current  
week (with a passed current day, say) and what then end date is at  
Saturday.


So take today: Tuesday March 17, 2009

I want to get:
Sunday March 15, 2009
Monday March 16, 2009
Tuesday March 17, 2009
Wednesday March 18, 2009
Thursday March 19, 2009
Friday March 20, 2009
Saturday March 21, 2009

Thanks!
-Jason

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



Re: [PHP] Calendar/Date

2009-03-17 Thread Paul M Foster
On Tue, Mar 17, 2009 at 08:52:11PM -0700, Jason Todd Slack-Moehrle wrote:

 Hi All,

 Does anyone have code and/or advice for how to get get the current
 week (with a passed current day, say) and what then end date is at
 Saturday.

 So take today: Tuesday March 17, 2009

 I want to get:
 Sunday March 15, 2009
 Monday March 16, 2009
 Tuesday March 17, 2009
 Wednesday March 18, 2009
 Thursday March 19, 2009
 Friday March 20, 2009
 Saturday March 21, 2009

I just answered a question similar to this. You might check the
archives. In this case, you'll need to use the getdate() function (see
php.net/manual/en/ for details) to get the array of values for today
(like the day of the month, month number, year, etc.). The getdate()
function returns an array, one of whose members is 'wday', which is the
day of the week, starting with 0 for Sunday. Use that number to
determine how many days to go back from today. Then use mktime() to get
the timestamps for each day in turn. You feed mktime() values from the
getdate() call. Then you can use strftime() or something else to print
out the dates in whatever format, given the timestamps you got.

Be careful in feeding values to mktime(). If your week spans a
month or year boundary, you'll need to compensate for it when giving
mktime() month numbers, day numbers and year numbers.

Paul

-- 
Paul M. Foster

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



[PHP] Calendar Date Help

2008-05-28 Thread Mark Weaver

Hi all,

I've put this off as long as possible, however I think I've reached an 
impasse.


I've got an application that I've been writing. One of the modules for 
this app is an event calendar. I've got the calendar to the place where 
it displays the current month as well as previous and future months. The 
 place I'm stuck is it will only show months in the past or the future 
that are months in the current year.


Basically the method I'm using to move backward and forward is with Unix 
timestamps.


1. When the calendar first loads the what is checked for;
// passed in via $_GET
$what == current, prev, or next
  a. current is the default
$now = time()
$prev = date('n',$now)-1
$next = date('n',$now)+1
  b. Timestamp values are then stored in an array and then
sent to client in session cookie which is then accessed
upon each subsequent request to display the event calendar.

My question/boggle is why, when the calendar advances to 
December(current year) it will display January, but of the current year. 
The same happens in reverse.


Once I reach the end of the year either in the past or future the month 
increases or decreases accordingly, but the year doesn't change. Since 
the year value isn't changing the month calendar days that are displayed 
simply repeat themselves.


I know there's something I'm missing, but I am definitely not seeing 
what it is...


/** code below /

$cal = new Calendar;
$calpos = array();

// check incoming values
if ($what === current){
$cal-setCal(0,0,0,date('n'),1);
$now = time();
$prev = $cal-getStamp(date('n',$now)-1,1);
$next = $cal-getStamp(date('n',$now)+1,1);
$calpos['curr'] = $now;
$calpos['prev'] = $prev;
$calpos['next'] = $next;
$_SESSION['calendar'] = $calpos;
}   
elseif($what === prev){
$peek = $_SESSION['calendar'];
$now = $peek['prev'];
$cal-setCal(0,0,0,date('n',$now),1);
$prev = $cal-getStamp(date('n',$now)-1,1);
$next = $cal-getStamp(date('n',$now)+1,1);
$calpos['curr'] = $now;
$calpos['prev'] = $prev;
$calpos['next'] = $next;
$_SESSION['calendar'] = $calpos;
}
elseif($what === next){
$peek = $_SESSION['calendar'];
$now = $peek['next'];
$cal-setCal(0,0,0,date('n',$now),1);
$prev = $cal-getStamp(date('n',$now)-1,1);
$next = $cal-getStamp(date('n',$now)+1,1);
$calpos['curr'] = $now;
$calpos['prev'] = $prev;
$calpos['next'] = $next;
$_SESSION['calendar'] = $calpos;
}


function setCal($h=0,$m=0,$s=0,$offset,$dayVal=1){  
  $stamp = date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y')));
  // Using the stamp the various necessary properties are set for the
  // object

function getStamp($dateStr,$dayVal=1){
  return date('U',mktime(0,0,0, $dateStr,$dayVal,date('Y')));
}

--

Mark
-
the rule of law is good, however the rule of tyrants just plain sucks!
Real Tax Reform begins with getting rid of the IRS.
==
Powered by CentOS5 (RHEL5)

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



Re: [PHP] Calendar Date Help

2008-05-28 Thread Robert Cummings

On Wed, 2008-05-28 at 14:27 -0400, Mark Weaver wrote:
 Hi all,
 
 I've put this off as long as possible, however I think I've reached an 
 impasse.
 
 I've got an application that I've been writing. One of the modules for 
 this app is an event calendar. I've got the calendar to the place where 
 it displays the current month as well as previous and future months. The 
   place I'm stuck is it will only show months in the past or the future 
 that are months in the current year.
 
 Basically the method I'm using to move backward and forward is with Unix 
 timestamps.
 
 1. When the calendar first loads the what is checked for;
   // passed in via $_GET
   $what == current, prev, or next
a. current is the default
   $now = time()
   $prev = date('n',$now)-1
   $next = date('n',$now)+1
b. Timestamp values are then stored in an array and then
   sent to client in session cookie which is then accessed
   upon each subsequent request to display the event calendar.
 
 My question/boggle is why, when the calendar advances to 
 December(current year) it will display January, but of the current year. 
 The same happens in reverse.
 
 Once I reach the end of the year either in the past or future the month 
 increases or decreases accordingly, but the year doesn't change. Since 
 the year value isn't changing the month calendar days that are displayed 
 simply repeat themselves.
 
 I know there's something I'm missing, but I am definitely not seeing 
 what it is...
 
 /** code below /
 
 $cal = new Calendar;
 $calpos = array();
   
 // check incoming values
 if ($what === current){
   $cal-setCal(0,0,0,date('n'),1);
   $now = time();
   $prev = $cal-getStamp(date('n',$now)-1,1);
   $next = $cal-getStamp(date('n',$now)+1,1);
   $calpos['curr'] = $now;
   $calpos['prev'] = $prev;
   $calpos['next'] = $next;
   $_SESSION['calendar'] = $calpos;
 } 
 elseif($what === prev){
   $peek = $_SESSION['calendar'];
   $now = $peek['prev'];
   $cal-setCal(0,0,0,date('n',$now),1);
   $prev = $cal-getStamp(date('n',$now)-1,1);
   $next = $cal-getStamp(date('n',$now)+1,1);
   $calpos['curr'] = $now;
   $calpos['prev'] = $prev;
   $calpos['next'] = $next;
   $_SESSION['calendar'] = $calpos;
 }
 elseif($what === next){
   $peek = $_SESSION['calendar'];
   $now = $peek['next'];
   $cal-setCal(0,0,0,date('n',$now),1);
   $prev = $cal-getStamp(date('n',$now)-1,1);
   $next = $cal-getStamp(date('n',$now)+1,1);
   $calpos['curr'] = $now;
   $calpos['prev'] = $prev;
   $calpos['next'] = $next;
   $_SESSION['calendar'] = $calpos;
 }
 
 
 function setCal($h=0,$m=0,$s=0,$offset,$dayVal=1){
$stamp = date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y')));
^
^
^
You don't want the current year. Fix this to a request year.

// Using the stamp the various necessary properties are set for the
// object
 
 function getStamp($dateStr,$dayVal=1){
return date('U',mktime(0,0,0, $dateStr,$dayVal,date('Y')));
 }
^
^
^
Similarly.

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


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



Re: [PHP] Calendar Date Help

2008-05-28 Thread Mark Weaver

Robert Cummings wrote:

function getStamp($dateStr,$dayVal=1){
   return date('U',mktime(0,0,0, $dateStr,$dayVal,date('Y')));
}

^
^
^
Similarly.

Cheers,
Rob.


Hi Rob,

Changing:
function setCal($h=0,$m=0,$s=0,$offset,$dayVal=1){  
   $stamp = date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y')));

To:
function setCal($h=0,$m=0,$s=0,$offset,$dayVal=1){
  if (date('Y')  date('Y',date('U',mktime($h,$m,$s,
 $offset,$dayVal,date('Y')
  {
 $stamp = date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y')+1));
  }
  else
  {
 $stamp = date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y')));
  }

Makes no change. Strange condition still exists. I was rather hoping
that if date('U',mktime($h,$m,$s, $offset,$dayVal,date('Y')))
is smart enough to know to increase or decrease the month value that it
would do the same to the year value accordingly.

$offset contain either date('n',$timestamp)-1  or  date('n',$timestamp)+1

The $timestamp value comes from the array being stored in the
$_SESSION['calendar'] as either 'current', 'prev', or 'next'.

--

Mark
-
the rule of law is good, however the rule of tyrants just plain sucks!
Real Tax Reform begins with getting rid of the IRS.
==
Powered by CentOS5 (RHEL5)


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