string array - strange behaviour

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
polarke
Forum Newbie
Posts: 3
Joined: Sat Aug 09, 2008 3:18 am

string array - strange behaviour

Post by polarke »

I am creating this 'message of the day' topic. Therefore I fill in first a string array.

$motd[1+1*31]="This is January 1st";
$motd[2+1*31]="This is January 2nd";
...
$motd[31+12*31]="lalala";

Then I select the message to write out using date(). I have now the problem, that starting from 5th of August, the strings for dates after are empty (they have however an initialisation line)

What can cause this and what can avoid this?

NB: I split up the list of initialisations using an if then else on (date("m")<8) and this moved forward the point were strings were starting to be empty with 5 days or so.

Thanks for any help.
filippo.toso
Forum Commoner
Posts: 30
Joined: Thu Aug 07, 2008 7:18 am
Location: Italy
Contact:

Re: string array - strange behaviour

Post by filippo.toso »

Why 1 + 1 * 31 is the 1st of January?

If you use date('z') as index, the 1st of January is 0, the 1st of February is 31, the 1st of March is 59 and so on.
polarke
Forum Newbie
Posts: 3
Joined: Sat Aug 09, 2008 3:18 am

Re: string array - strange behaviour

Post by polarke »

Yes, thats true, but it need to be correct for years with 28 days in February.
AND: The messages are coupled, so we need to deduct fast the real date from the nr.

But that's not the point, the point is: the string array is not working.
filippo.toso
Forum Commoner
Posts: 30
Joined: Thu Aug 07, 2008 7:18 am
Location: Italy
Contact:

Re: string array - strange behaviour

Post by filippo.toso »

polarke wrote:Yes, thats true, but it need to be correct for years with 28 days in February.
That's easy. Just create the array for leap year and then use the http://www.php.net/array_splice to remove the 29 February when you need to use the array against a non leap year.
polarke wrote:The messages are coupled, so we need to deduct fast the real date from the nr.
I' sorry, but I can't understand what you mean. With the index of the message you can easily get the unix timestamp of the day it refers to.
polarke wrote:But that's not the point, the point is: the string array is not working.
What you mean with "the string array is not working". If you execute print_r($motd) does it contains all the required values?
How do you access the values and get the wrong result (the initialisation line)?
polarke
Forum Newbie
Posts: 3
Joined: Sat Aug 09, 2008 3:18 am

Re: string array - strange behaviour

Post by polarke »

If you execute print_r($motd) does it contains all the required values?
No, the strings are empty from a specific index.
The print_r() prints indexes 32 to 38, then 31 (!- not in order, and 31 should be empty),
then continueing with 41, 42 etc...
Post Reply