PHP Tide Clock
- Saturday, January 19, 2008, 10:36
- Scripts
- 1,878 views
- 4 comments
Here’s a quick little script for predicting tides. Be sure and change the high tide time on line 2. You may have to play around with the numbers a little to get it working on your site. Please, don’t ever use this script for navigational purposes. Seriously, this is a rough estimate and not for marine navigation purposes.
Tides depend on many factors, sun, moon, wind and some other things I don’t understand. But I do understand tides differ by a set amount of time in most places – usually. I developed this code to work for Folly Beach, but it will work in locations with a 12:50 time difference between high tides. As a general rule, this works fairly well, but you will still need to update the tides occasionally or during time changes.
<!–
php tide by dawhoo
*********************************************************************
© dawhoo 2004
dawhoo.com
This script must be used in whole with credit in tact.
php tide script by philip nicholson
*********************************************************************
do not rely on this script for navigational purposes – serious
*********************************************************************
Instructions:
Paste into any php page an viola! There are the predicted next tides.
1)Change $knowntide to the last observed high tide
2)You may need to tweak the $knowntide to adjust for a server in a different time zone than where the tides are being measured.
–>
<?php
$knowntide = strtotime(‘2007-06-25 18:30′); // known hightide use the sample format
$current = date(U);
$interval = (12 * 3600) + (25 * 60); // 12 hours and 50 minutes between high tides
$num_tides_passed = floor(($current – $knowntide) / $interval);
$last_tide = $knowntide + ($interval * $num_tides_passed);
$next_high = $last_tide + (12 * 3600) + (25 * 60); // 12 hours and 50 minutes between 2 high tides
$next_low = $last_tide + (6 * 3600) + (13 * 60); ?> // 6 hours and 13 minutes between high and low
<?php echo date(‘m/d g:i a’, $next_low); ?> low // change “m/d g:i a” to use a different date format
<br><font color=”blue”>
<?php echo date(‘m/d g:i a’ , $next_high); ?> high
<br></font>
<?php echo date( ‘m/d g:i a’ , $next_low + (12 * 3600) + (25 * 60)); ?> low<br />
<font color=”blue”>
<?php echo date( ‘m/d g:i a’ , $next_high + (12 * 3600) + (25 * 60)); ?> high</font>
<br><br><a href=”http://www.dawhoo.com/script.html”>dawhoo tide</a>
About the Author
4 Comments on “PHP Tide Clock”
Write a Comment
Gravatars are small images that can show your personality. You can get your gravatar for free today!
Isn’t a htm file is needed to make this work?
When I click .php files from my website they will downloads to my home PC.
It would be helpful to show a complete example to viewers.
That is a complete example. Most of the time, a php_includes is used to call the function. Your hosting company may not have PHP enabled, which would be very rare. However, more often the case deleting the cache clears up the problem with PHP wanting to download rather than render.
// Based on this script I made this one. It needs some changes to show the exacts hours in
//each day, but its a begining
<?php
header('Content-Type: text/html; charset=utf-8');
function mostrar_form()
{
echo "”;
echo “Month:”;
echo “”;
echo “Year:”;
echo “”;
echo “”;
echo “”;
}
// Here you can translate the name of the months
function mes($m)
{
switch($m){
case “January”: return “Janeiro”; break;
case “February”: return “Fevereiro”; break;
case “Mars”: return “Março”; break;
case “April”: return “Abril”; break;
case “May”: return “Maio”; break;
case “June”: return “Junho”; break;
case “July”: return “Julho”; break;
case “August”: return “Agosto”; break;
case “September”: return “Setembro”; break;
case “Octuber”: return “Outubro”; break;
case “November”: return “Novembro”; break;
case “December”: return “Dezembro”; break;
}
}
function processa_form($mes, $dia, $ano)
{
$knowntide = strtotime(‘2010-04-28 03:00′); // Last known high tide–>
$current = date(‘U’, mktime(0, 0, 0, $mes,$dia, $ano));
$interval = (12 * 3600) + (21 * 60); // 12 hours and 50 minutes between high tides–>
$num_tides_passed = floor(($current – $knowntide) / $interval);
$last_tide = $knowntide + ($interval * $num_tides_passed);
$next_high = $last_tide + $interval; // 12 hours and 50 minutes between 2 high tides –>
$next_low = $last_tide + (6 * 3600) + (17 * 60); //
echo date(‘G:i’, $next_low),” Baixa-Mar “; // low tide
echo date(‘G:i’ , $next_high),” Preia-Mar “;//high tide
echo date(‘G:i’ , $next_low + $interval),” Baixa-Mar “;
echo date(‘G:i’ , $next_high + $interval),” Preia-Mar “;
}
mostrar_form();
if (empty($_POST['mes'])){
$date =time () ;
}
else
{
$month_year=$_POST['mes'].”/1/”.$_POST['ano'];
$date=strtotime($month_year);
}
//This gets today’s date
//This puts the day, month, and year in seperate variables
$day = date(‘d’, $date) ;
$month = date(‘m’, $date) ;
$year = date(‘Y’, $date) ;
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
//This gets us the month name
$title = date(‘F’, $first_day) ;
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date(‘D’, $first_day) ;
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
case “Sun”: $blank = 0; break;
case “Mon”: $blank = 1; break;
case “Tue”: $blank = 2; break;
case “Wed”: $blank = 3; break;
case “Thu”: $blank = 4; break;
case “Fri”: $blank = 5; break;
case “Sat”: $blank = 6; break;
}
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ;
//Here we start building the table heads
echo “”;
echo ” “, mes($title), ” $year “;
echo “DomingoSegundaTerçaQuartaQuintaSextaSábado”;
//This counts the days in the week, up to 7
$day_count = 1;
echo “”;
//first we take care of those blank days
while ( $blank > 0 )
{
echo ” “;
$blank = $blank-1;
$day_count++;
}
//sets the first day of the month to 1
$day_num = 1;
//count up the days, untill we’ve done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "”;
echo $day_num;
echo “”;
echo “”;
processa_form($month,$day_num, $year);
echo “”;
echo “”;
$day_num++;
$day_count++;
//Make sure we start a new row every week
if ($day_count > 7)
{
echo “”;
$day_count = 1;
}
}
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo " “;
$day_count++;
}
echo “”;
ooooh, I will have to check that out! I like how you deal with the day ahead – nice!