Jump to content
Larry Ullman's Book Forums

Struggling With An Idea


Recommended Posts

Dear Larry

 

I have been reading edition 3 of the VQG with great interest and my mind is boiling over with ideas for a website that I build from scratch. Before I read the book even.

 

Now. I am not through the book yet, but I was wondering if you could help me with the following.

The event is a yearly oldtimer event. Every first Sunday of September at 23:59 I should change a couple of terms on a website, so that these terms count for the next year.

Currently we are at the thirteenth event, which is on Sept 4th 2011

the word Thirteenth and the date are the variables I wish to print automatically every year.

 

I was thinking to build an array providing the words Thirteenth, Forteenth, etc, etc

But how can I print the 1st Sunday of the next year

AND

Change this automatically on the first Sunday of the current year at 23:59

 

Is this possible at all?

 

Thanks a lot for your inspiring writing! I am ploughing through this one and then on to the php6 and mysql 5 book. It's already waiting for me :)

 

Regards

Mike

Link to comment
Share on other sites

First off I appreciate if you're learning you want to make everything dynamic and do it with PHP but to do exactly what you want 100% dynamically is quite difficult - especially when (if I understand correct) the value will only change once every year? If that's the case you might want to compromise by simply creating an array with the timestamp as the key and the string to output as the value. That way you could could just look for the lowest key greater than the current timestamp and output it. You could quickly populate the next 10, 15 or 20 years and not have to worry about anything too complex.

 

I don't know of and can't think of any simple dynamic way to convert from 13 to thirteen - you could do would be 13th either using a series of logic operators based on the day or a combination of mktime() and date() using the S formatter.

 

If you wanted to dynamically calculate the first Sunday of the next September you can use the strtotime() function - although I'm sure I've run into problems in the past using 1st x'day when the that day falls on the first day of the month. I think the solution was to use '1 sunday september' but it was a long time ago.

 

Hope some of that helps

Link to comment
Share on other sites

Hello Stuart

 

Thans for your reply. I also consulted a 19 year old nephew of my wife ;) Very very smart kid. He's way ahead of me in PHP (why would I be reading the books otherwise)

We figured something out that seems to work. And I would like to share that code if you allow me. Some words are in Dutch, but they don't matter for the script itself anyway.

 

He made the basic script. I was able to turn it into functions after reading the last paragraphs of chapter 10 about globalizing. And we got it to work!

 

I stored this code in the header.php I use and I call the functions when I need them in my bodies.

 

Does the script make any sense?

 

Thanks

Mike

 

<?php

date_default_timezone_set('Europe/Amsterdam');

function date_in_words($num)

{

switch($num)

{

case 13: return "dertiende";

case 14: return "veertiende";

case 15: return "vijftiende";

case 16: return "zestiende";

case 17: return "zeventiende";

case 18: return "achtiende";

case 19: return "negentiende";

case 20: return "twintigste";

case 21: return "eentwintigste";

case 22: return "tweeentwintigste";

case 23: return "drieentwintigste";

 

}

}

 

 

$day=0; //$day store the value 1 (first day in month) (for some reason I figured out we had to change the value to 0, as it skipped if it was actually a FIRST of September

$month=9; //$month store the value 9 (september)

$year_long=date("Y"); //$year_long store the value of current year of server in long format (example "2011")

$year_short=date("y"); //$year_short store the value of current year of server in short format (example "11")

 

 

$date=strtotime("$day.$month.$year_long"); //$date store the timestamp for first day of steptember of current year

 

$p=strtotime("next sunday", $date);//$p store the timestamp of next sunday from $date (that means it stores the timestamp of first sunday of september)

 

$data_event=strtolower(date("j F Y ", $p)); //$data store the date of first sunday of september (we convert the timestamp in date format d-m-Y)

$current_date=strtotime(date("j F Y"));

 

function print_event() {

global $current_date;

global $p;

global $year_short;

 

if($p<$current_date)

{

$day_after=date("Y",strtotime("next year"));

print date_in_words($year_short+3) ." "; //number of oldtimer edition is the year in short format +2

}

else

{

print date_in_words($year_short+2) ." "; //number of oldtimer edition is the year in short format +2

}

}

function print_date() {

global $current_date;

global $p;

global $year_short;

global $day;

global $month;

global $data_event;

 

if($p<$current_date)

{

$day_after=date("Y",strtotime("next year"));

print strtolower(date("j F Y ", strtotime("next sunday", strtotime("$day.$month.$day_after"))));

}

else

{

print $data_event;

}

}

 

?>

Link to comment
Share on other sites

Slightly annoyingly I've already written this post once and accidentally pressed refresh so apologies if it's not overly thorough! Thanks for sharing your code its always interesting to see how other people code as my experience thus far is Larry's books or my own. In the main I like the concept and the way you've gone about it - if you don't mind though I've noticed a few things that could be improved.

 

1) You've used the global keyword to provide functional level scope to all your variables. Global should only really be used to provide access to variables that do not change from call to call e.g. a database connection. With variables like yours it would be better practice to pass them as arguments to the function.

 

2) You declare $day_after inside print_event but the variable is never used.

 

3) I think the process of getting the 1st Sunday of the next September (I have got that right haven't I?) is a little long winded. You could achieve the same with:

 

if (($ts = strtotime("sunday 0 september")) < time()){

$ts = strtotime("sunday 0 september next year");

}

 

Then to get the variable to plug into the date_in_words function just use:

 

-1998 + date('Y', $ts);

 

Hope you don't mind me bringing those things up - their only minor things but they make a nice concept a little more elegant.

Link to comment
Share on other sites

Hi Stuart

 

No I don't mind this feedback at all. And will also pass it to my "assistant".

 

1. I will dig into that :) Still a beginner, but I get what you mean.

 

2. I was already wondering myself how some variables never return. But since the thing works ;)

 

3. Very nice! This one will be appeciated by my guy!

 

Thanks again for your time helping me out!

 

I'm having a ball making things up like this and actually seeing them into action. Eagerly learning PHP whenever I can

Regards

 

Mike

Link to comment
Share on other sites

 Share

×
×
  • Create New...