bgareth Posted November 20, 2012 Share Posted November 20, 2012 Hello, Please could you give me a bit of guidance on the following script, half of it is appearing (as code) in my browser on my Wamp installation! <html> <head> <title>PHP Christmas Message Challenge</title> </head> <body> <?php /*Array assignment*/ $day = date("d"); $month = date("m"); $hour = date("H"); /* To determine whether it is Christmas Eve the following day and if so to display a message after 7pm on 23 December. */ if ($day == 23 and $month == 12 and $hour > 19) { echo "Christmas Eve is tomorrow."; } /* To determine whether it is Christmas Day the following day and if so to display a message after 7pm on 24 December. */ elseif ($day == 24 and $month == 12 and $hour > 19) { echo "Christmas Day is tomorrow."; } /* Message to display if it is neither Christmas Eve or Christmas Day the following day. */ else { echo "Tomorrow is a normal day."; } ?> </body> </html> Kind Regards, BG. Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/ Share on other sites More sharing options...
deoiub Posted November 20, 2012 Share Posted November 20, 2012 What is appearing in your browser? Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393741 Share on other sites More sharing options...
Beeeeney Posted November 20, 2012 Share Posted November 20, 2012 Just tested it, seems fine to me. Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393744 Share on other sites More sharing options...
bgareth Posted November 20, 2012 Author Share Posted November 20, 2012 Wow Beeeney it's working, you share my nickname in real life btw! See i'm learning quick :-p Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393750 Share on other sites More sharing options...
bgareth Posted November 20, 2012 Author Share Posted November 20, 2012 Is it deff working properly, not sure how to make it trigger the christmas messages when it isn't christmas to test it... Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393751 Share on other sites More sharing options...
Beeeeney Posted November 20, 2012 Share Posted November 20, 2012 Is it deff working properly, not sure how to make it trigger the christmas messages when it isn't christmas to test it... Change the date on the script to today? Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393757 Share on other sites More sharing options...
bgareth Posted November 20, 2012 Author Share Posted November 20, 2012 So delete the assignment of the variable to date function and simply set the variable to "23" "12" "1900" respectively? Is that the format of the time variable stored? Is my code rubbish and are there any improvements to make it more professional? Thanks for your help :-) Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393758 Share on other sites More sharing options...
Beeeeney Posted November 20, 2012 Share Posted November 20, 2012 (edited) It's the most basic PHP code possible. I can only think of one task that would be more basic and that would be to echo "Hello, world!" into the browser. I just meant change your elseif statement to today's date and see if it echos "Christmas day is tomorrow". Also, remove the $hour variable, you don't need it. Edited November 20, 2012 by Beeeeney Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393776 Share on other sites More sharing options...
bgareth Posted November 20, 2012 Author Share Posted November 20, 2012 (edited) Well people have to start somewhere and that is quite discouraging. I'm trying to learn. Considering I only really started trying to learn a couple of days ago, surely I have done quite well to get this far, particularly as, save for some basic CSS and HTML, I have not coded before. When I used the word "professional" I meant efficient and I was essentially asking if it was written in the same way as anyone with a good rounded knowledge of PHP would write it. I have advanced the script slightly and I would like to add in some other holidays. I also feel that my echo format for the current time and date is way too long, there must be a way to shorten it. Further, to add in the extra holidays is it more appropriate to use an array, to write an if statement for each one or is there another way? Regards, BG. <!DOCTYPE html> <html lang="en"> <head> <title>English Holiday Checker</title> <!-- Inline CSS (in case you were wondering!) --> <style> /* Collaborative Header CSS */ h1 {text-align:center; color:black; font-family:"Arial", Calibri, "sans-serif";} /* Collaborative Paragraph CSS */ p {margin-left: 20px; font-family:"Arial", "Calibri", "sans-serif"; color:blue;} </style> </head> <body> <!-- Header for page --> <h1>English Holiday Checker</h1> <p> <?php /* Variable declarations */ $weekday = date("D"); $od = date("S"); $day = date("d"); $month = date("M"); $year = date("Y"); $hour = date("H"); $minute = date("i"); $seconds = date("s"); /* Display current time and date */ echo "<p>The time when you loaded this page was: "; echo $hour; echo ":"; echo $minute; echo ":"; echo $seconds; echo " GMT"; echo " and the date was "; echo $weekday; echo ", "; echo $day; echo $od; echo " "; echo $month; echo " "; echo $year; echo "."; echo "</p>"; echo "<p>I haven't learnt Javascript yet so I can't set it to auto-update without using meta-refresh.</p>"; /* To determine whether it is Christmas Eve the following day and if so to display a message after 7pm on 23 December. */ if ($day == 23 and $month == 12 and $hour > 19) { echo "<p>Christmas Eve is tomorrow.</p>"; } /* To determine whether it is Christmas Day the following day and if so to display a message after 7pm on 24 December. */ elseif ($day == 24 and $month == 12 and $hour > 19) { echo "<p>Christmas Day is tomorrow.</p>"; } /* Message to display if it is neither Christmas Eve or Christmas Day the following day. */ else { echo "<p>We are not in the Christmas period.</p>"; } ?> </p> </body> </html> Edited November 20, 2012 by bgareth Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393825 Share on other sites More sharing options...
Beeeeney Posted November 20, 2012 Share Posted November 20, 2012 Well people have to start somewhere and that is quite discouraging. I'm trying to learn. Considering I only really started trying to learn a couple of days ago, surely I have done quite well to get this far, particularly as, save for some basic CSS and HTML, I have not coded before. When I used the word "professional" I meant efficient and I was essentially asking if it was written in the same way as anyone with a good rounded knowledge of PHP would write it. I have advanced the script slightly and I would like to add in some other holidays. I also feel that my echo format for the current time and date is way too long, there must be a way to shorten it. Further, to add in the extra holidays is it more appropriate to use an array, to write an if statement for each one or is there another way? Regards, BG. <!DOCTYPE html> <html lang="en"> <head> <title>English Holiday Checker</title> <!-- Inline CSS (in case you were wondering!) --> <style> /* Collaborative Header CSS */ h1 {text-align:center; color:black; font-family:"Arial", Calibri, "sans-serif";} /* Collaborative Paragraph CSS */ p {margin-left: 20px; font-family:"Arial", "Calibri", "sans-serif"; color:blue;} </style> </head> <body> <!-- Header for page --> <h1>English Holiday Checker</h1> <p> <?php /* Variable declarations */ $weekday = date("D"); $od = date("S"); $day = date("d"); $month = date("M"); $year = date("Y"); $hour = date("H"); $minute = date("i"); $seconds = date("s"); /* Display current time and date */ echo "<p>The time when you loaded this page was: "; echo $hour; echo ":"; echo $minute; echo ":"; echo $seconds; echo " GMT"; echo " and the date was "; echo $weekday; echo ", "; echo $day; echo $od; echo " "; echo $month; echo " "; echo $year; echo "."; echo "</p>"; echo "<p>I haven't learnt Javascript yet so I can't set it to auto-update without using meta-refresh.</p>"; /* To determine whether it is Christmas Eve the following day and if so to display a message after 7pm on 23 December. */ if ($day == 23 and $month == 12 and $hour > 19) { echo "<p>Christmas Eve is tomorrow.</p>"; } /* To determine whether it is Christmas Day the following day and if so to display a message after 7pm on 24 December. */ elseif ($day == 24 and $month == 12 and $hour > 19) { echo "<p>Christmas Day is tomorrow.</p>"; } /* Message to display if it is neither Christmas Eve or Christmas Day the following day. */ else { echo "<p>We are not in the Christmas period.</p>"; } ?> </p> </body> </html> I wasn't trying to discourage you at all. I was merely stating that there isn't really another way your code could be written. You can have more than one parameter inside the date(); function. http://php.net/manual/en/function.date.php Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393830 Share on other sites More sharing options...
bgareth Posted November 20, 2012 Author Share Posted November 20, 2012 (edited) Thanks, I don't know how long you have been coding but if you remember back to when you started, it is hard enough to make your code run without errors :-) I appreciate your help so far and your a decent enough guy to post back. You see all those echo statements (19 of them), there must be a way of shortening them? I had already looked at the link you suggested but i'm not sure how I can join them up (presumably this is what you are referring to when you say you can have more than one parameter inside the date() function)? Edit - ah I didn't see this bit: // Prints something like: Monday 8th of August 2005 03:12:46 PM echo date('l jS \of F Y h:i:s A'); Is that an example of what you are suggesting, I notice the /of Is that how to incorporate bits of text in lines of PHP? I didn't realise escape could be used for anything other than apostrophes and double quotes.. Edited November 20, 2012 by bgareth Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393831 Share on other sites More sharing options...
Muddy_Funster Posted November 20, 2012 Share Posted November 20, 2012 you meen shorten it like this? echo "<p>The time when you loaded this page was: $hour:$minute:$seconds GMT. and the date was $weekday, $day $od $month $year.</p><p>I haven't learnt Javascript yet so I can't set it to auto-update without using meta-refresh.</p>"; if you create a string using double quotes any variables that you put into it (such as $hour) will be parsed (translated into it's contents) during execution of the script. using single quotes will not do this, it will create a string exatly as it is on the code page. Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393832 Share on other sites More sharing options...
Beeeeney Posted November 20, 2012 Share Posted November 20, 2012 (edited) Thanks, I don't know how long you have been coding but if you remember back to when you started, it is hard enough to make your code run without errors :-) I appreciate your help so far and your a decent enough guy to post back. You see all those echo statements (19 of them), there must be a way of shortening them? I had already looked at the link you suggested but i'm not sure how I can join them up (presumably this is what you are referring to when you say you can have more than one parameter inside the date() function)? I consider myself an amateur at best. Still way too much to learn and not enough time to learn it. From PHP.net: // Prints something like: Monday 8th of August 2005 03:12:46 PM echo date('l jS \of F Y h:i:s A'); Just play with it a little bit. Edited November 20, 2012 by Beeeeney Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393834 Share on other sites More sharing options...
Muddy_Funster Posted November 20, 2012 Share Posted November 20, 2012 (edited) That may not work exactly like that, what you may need to do is wrap the variables that have no spaces around them in curly braces {} just to make certain that the parser knows to treat them as variables. so "...{$hour}:{$minute}:{$second}...". Curly braces should always be used when echoing array values like this, if there is space around them or not Edt : am reffering to my last post - not Beeeeney's (is that the right number of e's? O_o) Edited November 20, 2012 by Muddy_Funster Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393835 Share on other sites More sharing options...
bgareth Posted November 20, 2012 Author Share Posted November 20, 2012 With respect to adding in the extra holidays, is an array appropriate or an if statement or even another method you can suggest? I'm looking at adding in things like St Patrick's Day, Bonfire Night etc. Thanks to Muddy_Funster, also to you Beeeeeney, what you have both said so far makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393837 Share on other sites More sharing options...
Muddy_Funster Posted November 20, 2012 Share Posted November 20, 2012 you would probably want to use an array to hold the dates of interest. this way you can loop through the array and check off the dates without having to code an if statement for every date you want to check. Problem is, arrays can be a bit of a mind bender when you first come up against them, and this would be best done with a multidimensional arra, which is to say, an array of arrays. If your game to get into arrays just now and you work best in a "in at the deap end and fight to swim" kind of way then that's what to go for. My suggestion would be to leave this where you have it and get your feet a bit damper with the basics first, then have a dive in if you feel confident. Whatever way you feel like going with it there are a lot of good people on here that will do their best to help keep your head above water. Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393838 Share on other sites More sharing options...
bgareth Posted November 20, 2012 Author Share Posted November 20, 2012 That's very kind :-) Is there an "idiot's guide" to multi-dimensional arrays and what does it mean in English if you were to put the term in laymen's terms :-) I'm inspired by setting myself goals to do things that I know i'll use, i'm setting myself tasks as I read the SAMS Teach Yourself book I have :-) I'm no good with the "official" reference materials on php.net - at this stage a lot of it is gobbledygook on that site! Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393842 Share on other sites More sharing options...
Beeeeney Posted November 20, 2012 Share Posted November 20, 2012 That's very kind :-) Is there an "idiot's guide" to multi-dimensional arrays and what does it mean in English if you were to put the term in laymen's terms :-) I'm inspired by setting myself goals to do things that I know i'll use, i'm setting myself tasks as I read the SAMS Teach Yourself book I have :-) I'm no good with the "official" reference materials on php.net - at this stage a lot of it is gobbledygook on that site! I started learning with a video tutorial by some guy named Kevin Skoglund. He explains it in the plainest of English. Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393845 Share on other sites More sharing options...
Jessica Posted November 20, 2012 Share Posted November 20, 2012 One dimensional array: $foo['bar'] = 'foobar'; Two dimensional array $foo['bar'][1] = 'foobar1'; Three dimensions $foo['bar'][1][$arg] = 'foobar1arg'; etc. $holidays = array(); $holiday = array(); $holiday['name'] = 'Christmas'; $holiday['month'] = '12'; $holiday['day'] = '25'; $holiday['celebrateEve'] = true; $holiday['phrases'] = array('Ho Ho Ho!', 'Merry Christmas!', 'Joy to the World!'); $holidays[] = $holiday; echo '<pre>'; print_r($holidays); Run that and see if you get it now. Add to it. Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393862 Share on other sites More sharing options...
bgareth Posted November 21, 2012 Author Share Posted November 21, 2012 Hello, i've shortened the echo statement as suggested above and it works! Moving forward with the multidimensional array, so far this is what i've done: /* Multidimensional array for holidays - Christmas will be added later.*/ $holidays = array( array("Boxing_Day", 2612), array("Easter_Monday", 0104), array("Good_Friday", 2903), array("January_2nd", 0201), array("May_Day_Holiday", 0605), array("New_Year's_Day", 0101), array("New_Year's_Eve", 3112), array("Orangeman's_Day", 1207), array("Saint_Patrick's_Day", 1703), array("Spring_Bank_Holiday", 2705), array("St_Andrew's_Day", 3011), array("Summer_Bank_Holiday_Scotland", 0508), array("Summer_Bank_Holiday_ENIW", 2608), ); For example, Summer_Bank_Holiday_Scotland has a value of 0508 which is the 5th August. Is this correct so far and if so how do I write the loop that Muddy_funster is proposing above? Thanks again :-) Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393946 Share on other sites More sharing options...
bgareth Posted November 21, 2012 Author Share Posted November 21, 2012 Hello Jessica, thanks for your message, can it be done the way I have posted above or would it have to be done the way you have suggested? Thanks for taking the time to add to this :-) Kind Regards, BG. Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393947 Share on other sites More sharing options...
Jessica Posted November 21, 2012 Share Posted November 21, 2012 Good idea, make what I wrote but way harder to read. Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393960 Share on other sites More sharing options...
Barand Posted November 21, 2012 Share Posted November 21, 2012 Here's a function to calc English Bank holidays for a given year (Easy enough to add St. Patrick's (Mar 17) etc if required) [code] <?php function EnglishHols($year) { $englishHolidays = array(); $t = mktime(0,0,0,1,1,$year); $d = date('Y-m-d', $t); $englishHolidays[$d] = 'New Year\'s Day'; $t = easter_date($year); $d = date('Y-m-d', strtotime('-2 days', $t)); $englishHolidays[$d] = 'Good Friday'; $d = date('Y-m-d', $t); $englishHolidays[$d] = 'Easter Sunday'; $d = date('Y-m-d', strtotime('+1 days', $t)); $englishHolidays[$d] = 'Easter Monday'; $t = mktime(0,0,0,6,1,$year); $d = date ('Y-m-d', strtotime('last monday', $t)); // last monday in may $englishHolidays[$d] = 'Spring Bank Holiday'; $t = mktime(0,0,0,9,1,$year); $d = date ('Y-m-d', strtotime('last monday', $t)); // last monday in august $englishHolidays[$d] = 'August Bank Holiday'; $t = mktime(0,0,0,12,25,$year); $d = date ('Y-m-d', $t); $englishHolidays[$d] = 'Christmas Day'; $t = mktime(0,0,0,12,26,$year); $d = date ('Y-m-d', $t); $englishHolidays[$d] = 'Boxing Day'; return $englishHolidays; } $hols = EnglishHols(2013); echo '<pre>'.print_r($hols, 1).'</pre>'; ?> RESULTS Array ( [2013-01-01] => New Year's Day [2013-03-29] => Good Friday [2013-03-31] => Easter Sunday [2013-04-01] => Easter Monday [2013-05-27] => Spring Bank Holiday [2013-08-26] => August Bank Holiday [2013-12-25] => Christmas Day [2013-12-26] => Boxing Day ) Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1393961 Share on other sites More sharing options...
bgareth Posted November 21, 2012 Author Share Posted November 21, 2012 Good idea, make what I wrote but way harder to read. Hello Jessica :-) Which one do you think is harder? Sen, Thanks for this - 1. You have declared the variable $year without content? Can you declare the content later? function EnglishHols($year) 2. What is going on with the $t variable here? It seems that it is being reassigned in the following section? $t = easter_date($year); $d = date('Y-m-d', strtotime('-2 days', $t)); $englishHolidays[$d] = 'Good Friday'; $d = date('Y-m-d', $t); $englishHolidays[$d] = 'Easter Sunday'; $d = date('Y-m-d', strtotime('+1 days', $t)); $englishHolidays[$d] = 'Easter Monday'; 3. you have used the mktime function, what is the difference between that and the way I have specified dates and times so far and what is each properly used for? Thanks and Kind Regards, BG. Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1394234 Share on other sites More sharing options...
Barand Posted November 21, 2012 Share Posted November 21, 2012 1. You have declared the variable $year without content? Can you declare the content later? function EnglishHols($year) I defined the function as having $year as its argument. Its value is defined when you call the function 2. What is going on with the $t variable here? It seems that it is being reassigned in the following section? Yes. I've finished with it by the time I get to the next section. 3. you have used the mktime function, what is the difference between that and the way I have specified dates and times so far and what is each properly used for? Some of the dates required a unix timestamp for the calculations. I used it in the other dates just for the sake of consistency Quote Link to comment https://forums.phpfreaks.com/topic/270937-date-script-help-needed-please/#findComment-1394237 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.