master4g Posted September 13, 2011 Share Posted September 13, 2011 Hey everyone. Currently I have a site set up where I manually edit the txt file and the site pulls it from the text file and displays via php. I wanted to automate it by creating one file which contains the sunset times for the whole year, and have php determine todays date and then display the corresponding sunset time. Can someone please point me in the right direction? maybe it can be set up in a way where each line in the txt file represents the day of the year. ex: Jan 1 would be line 0, Feb 1's sunset time would be on line 31 etc. Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 13, 2011 Share Posted September 13, 2011 A better approach would be to use a database. Do you have any experience with a database? Also, how are you getting the data? That will help to determine the best approach for importing/using the data in your script. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted September 13, 2011 Share Posted September 13, 2011 Look at these functions; I suspect they may come in handy. date_sunrise date_sunset date_sun_info Quote Link to comment Share on other sites More sharing options...
xyph Posted September 13, 2011 Share Posted September 13, 2011 What happens on a leap year? Why not use an INI to store it? [jan] 1=7:00 2=7:03 ;etc [feb] 1=8:00 2=8:03 ;etc Then use parse_ini_file() with the second argument TRUE http://php.net/manual/en/function.parse-ini-file.php Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted September 13, 2011 Share Posted September 13, 2011 Pikachu beat me to it, PHP does this for you already. -Dan Quote Link to comment Share on other sites More sharing options...
master4g Posted September 13, 2011 Author Share Posted September 13, 2011 Thanks for all the replies. I am relatively new to PHP so some of those suggestions flew past my head. I currently have a paper with the desired times for each day of the year, which I can sit down and input into the computer depending on how I decide to store the info. I don't have experience with databases but If I was pointed in the right direction, I can research and try to figure it out. xyph: can you please elaborate on using an INI to store? do you have any link or tip I can follow to look into that. Thanks Also, I can use the php sunset feature, but I would like to learn another way (like the txt file with the data, or an INI? or database) because hopefully in the future I can use this coding for another similar project which goes as follows: example: show a certain time for a certain date frame so from Jan1-Jan19 show "6:30" and then from Jan20-Feb23 show "6:45" , etc. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 13, 2011 Share Posted September 13, 2011 @master4g: Look at Pikachu2000's response. There are built-in functions in PHP (I didn't realize this) that will return the sunrise, sunset, etc. information AUTOMATICALLY. You do not need to do anything - no data entry. PHP is all knowing! Well, not really, but it is simply a mathematical calculation for the sunrise/sunset for any given latitude/longitude. Look at the functions he linked to Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted September 13, 2011 Share Posted September 13, 2011 To actual answer your more direct question: This is a very basic PHP/SQL question. Create a table in MySQL with 4 columns: Start, End, Sunrise, Sunset Start and End should be of DATETIME format. When you input your data, make sure the records always butt up against each other and do not overlap: 2011-09-11 00:00:00, 2011-09-13 23:59:59, 6:45, 7:12 2011-09-14 00:00:00, 2011-09-17 23:59:59, 6:53, 7:22 Have an HTML form with a date picker or an input box (for testing, you want date formats like YYYY-MM-DD). The PHP script that queries the database should accept the inputted date, format it to YYYY-MM-DD format (if necessary), and perform the query: SELECT Sunrise, Sunset FROM thatTableYouMade WHERE '{$theInput}' BETWEEN Start AND End; The result, which should be one row, will have the sunrise and sunset time. -Dan Quote Link to comment Share on other sites More sharing options...
master4g Posted September 13, 2011 Author Share Posted September 13, 2011 Ah ha, exactly what I was looking for. Thanks a lot for the detailed response. My followup question would be, would it be possible for the system to automatically chose todays date? Quote Link to comment Share on other sites More sharing options...
xyph Posted September 13, 2011 Share Posted September 13, 2011 Of course. A pure MySQL solution SELECT sunrise, sunset FROM table WHERE date = CURDATE() http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_curdate Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 13, 2011 Share Posted September 13, 2011 My followup question would be, would it be possible for the system to automatically chose todays date? Did you LOOK at the functions linked? They take a timestamp, so time() is all you need to supply Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted September 14, 2011 Share Posted September 14, 2011 mjdamato, as much as I hate to turn your posts back at you, but did you look at what he said? he doesn't want to use the sunrise and sunset functions because this is a thought experiment about displaying any data for a specific date based on date ranges stored in a table. master4g, given the table structure I gave you earlier, a query for "sunrise and sunset today" would be: SELECT Sunrise, Sunset FROM thatTableYouMade WHERE NOW() BETWEEN Start AND End; -Dan Quote Link to comment 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.