Twitch Posted October 7, 2008 Share Posted October 7, 2008 Hey guys, newbie to PHP and have had enough of pulling my hair out so I thought I'd ask for help. I'm trying to write a simple script to get the local machine's time and then change an include file based on whether it's 10 pm or not. For instance, it looks at the users time, then if it's passed 10 pm it changes to the next include file such as "friday.php" Thought it should be simple, but sometimes it appears to work and then I realize it doesn't. I'd done various versions of this, but here is what I have at the moment. Any help is much appreciated. <?php //code ?> <script type="text/javascript"> function Gettime() { var now = new Date(); document.write(now); } </script> <?php $time_current = "<script language=javascript>Gettime();</script>"; echo "time: ". $time_current; $d=date("D"); $h=date("h"); $m=date("i"); $a=date("A"); if ($d=="Mon") echo "Have a nice Monday!"; elseif ($d=="Tue") echo "Have a nice Tuesday!"; elseif ($d=="Wed") echo "Have a nice Wednesday!"; elseif ($h<="22" && $m<="00" && $d=="Thu") include('thursday.php'); elseif ($h<="22" && $m<="00" && $d=="Fri") include('friday.php'); elseif ($d=="Sat") echo "Have a nice Saturday!"; else echo "Have a nice Sunday!"; ?> Thursday and Friday are my test days at the moment. Thanks in advance, Twitch Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/ Share on other sites More sharing options...
btherl Posted October 7, 2008 Share Posted October 7, 2008 You mention "the user's time" and "the local machine's time". The time for your php script (the server's time) won't always match the time for javascript (user's time). That aside, if you want to move the end of the day to 10 pm instead of 12 midnight, you could try adding 2 hours to the time. I don't know how that will interact with daylight savings but it should work most of the time. print date("D H:i:s\n", strtotime("+2 hours")); Then all you need to check is the day name. Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/#findComment-658656 Share on other sites More sharing options...
Twitch Posted October 7, 2008 Author Share Posted October 7, 2008 You mention "the user's time" and "the local machine's time". The time for your php script (the server's time) won't always match the time for javascript (user's time). That aside, if you want to move the end of the day to 10 pm instead of 12 midnight, you could try adding 2 hours to the time. I don't know how that will interact with daylight savings but it should work most of the time. print date("D H:i:s\n", strtotime("+2 hours")); Then all you need to check is the day name. Actually I'm trying to get the local machine's time and not the server, I read somewhere that php only gets the server time therefore you need to use a combination of javascript and php. I was trying to change the php file at 10 pm and have the script check the day and time and change to the appropriate php include file. I guess I could see if it works by cycling through each day at midnight or change the end of day to 10 instead of midnight. How would I make it change the php file using your end of day script? Appreciate the prompt response earlier. -Twitch Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/#findComment-658661 Share on other sites More sharing options...
btherl Posted October 7, 2008 Share Posted October 7, 2008 If you need the javascript time it's a bit more complicated. The problem is that javascript only runs after the php script is finished. So you need to run a second php script which receives the time back from javascript. Or you can ask the user to set their timezone, OR you can try to auto-detect the timezone based on things such as IP address. What I wrote only works with server time (though once you have the user's time you can use it in a similar way). Anyway, there's 2 options I can think of 1. Use ajax to send back the time from the user 2. Submit the time from javascript via a form The second is probably easier Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/#findComment-658663 Share on other sites More sharing options...
Twitch Posted October 7, 2008 Author Share Posted October 7, 2008 Once again thanks so much for the quick response. Complicated is what I don't want...ha ha. What if I just get the server time and use a script like this. This is the one I originally had. The problem is I can't really test it easily since I can't manually change the time on the server. Therefore I have to wait for each day to go by...ha ha. I thought I'd post it here so a pro PHPer could look at it and at least see if it's proper code. <?php echo(date("l F Y h:i:s A") . "<br />"); ?> <?php $d=date("D"); $h=date("h"); $m=date("i"); $a=date("A"); if ($d=="Mon") echo "Have a nice Monday!"; elseif ($d=="Tue") echo "Have a nice Tuesday!"; elseif ($d=="Wed") echo "Have a nice Wednesday!"; elseif ($h<="10" && $m<="00" && $a="PM" && $d=="Thu") include('thursday-inc.php'); elseif ($h<="10" && $m<="00" && $a="PM" && $d=="Fri") include('friday-inc.php'); elseif ($d=="Sat") echo "Have a nice Saturday!"; else echo "Have a nice Sunday!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/#findComment-658688 Share on other sites More sharing options...
CroNiX Posted October 7, 2008 Share Posted October 7, 2008 Not dealing with your issue, but a way to shorten your code a bit.... You also forgot to use == in your $a="PM" <?php echo date("l F Y h:i:s A") . "<br />"; $d=date("l"); //changed this to use full weekday name $h=date("h"); $m=date("i"); $a=date("A"); if($d != 'Thursday' && $d !='Friday') { echo "Have a nice $d!"; } elseif($h<="10" && $m<="00" && $a=="PM" && $d=="Thursday") { include('thursday-inc.php'); } elseif ($h<="10" && $m<="00" && $a=="PM" && $d=="Friday") include('friday-inc.php'); } Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/#findComment-658700 Share on other sites More sharing options...
CroNiX Posted October 7, 2008 Share Posted October 7, 2008 I forgot a { after $d=="Friday") Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/#findComment-658705 Share on other sites More sharing options...
Twitch Posted October 7, 2008 Author Share Posted October 7, 2008 Appreciate it CroNIX, but in the final code the "Have a nice Saturday", "Have a nice Sunday" will be replaced with include('saturday-inc.php'); include('sunday-inc.php'); etc. Basically the include file is event information that changes by day. Just looking to automate the process rather than going in and changing the name of the include file manually. Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/#findComment-658710 Share on other sites More sharing options...
R0bb0b Posted October 7, 2008 Share Posted October 7, 2008 You may have already changed your mind but this will auto submit the data to a server side script, just change the action <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>JS Get Time</title> <script> function setClientTime() { var d = new Date(); var hours = d.getHours(); var mins = d.getMinutes(); var secs = d.getSeconds(); document.getElementById("hdnHour").value = hours; document.getElementById("hdnMin").value = mins; document.getElementById("hdnSec").value = secs; document.frmTime.submit(); } </script> </head> <body onload="setClientTime();"> <form name="frmTime" action="serversidescript.php" method="post"> <input type="hidden" name="hdnHour" id="hdnHour" /> <input type="hidden" name="hdnMin" id="hdnMin" /> <input type="hidden" name="hdnSec" id="hdnSec" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/#findComment-658711 Share on other sites More sharing options...
CroNiX Posted October 7, 2008 Share Posted October 7, 2008 Appreciate it CroNIX, but in the final code the "Have a nice Saturday", "Have a nice Sunday" will be replaced with include('saturday-inc.php'); include('sunday-inc.php'); etc. Basically the include file is event information that changes by day. Just looking to automate the process rather than going in and changing the name of the include file manually. Ah, can even make it shorter then.... <?php echo date("l F Y h:i:s A") . "<br />"; $d=strtolower(date("l")); //make day of week all lower case $h=date("h"); $m=date("i"); $a=date("A"); if($h<="10" && $m<="00" && $a=="PM") include($d . "-inc.php"); } Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/#findComment-658714 Share on other sites More sharing options...
Twitch Posted October 7, 2008 Author Share Posted October 7, 2008 You may have already changed your mind but this will auto submit the data to a server side script, just change the action <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>JS Get Time</title> <script> function setClientTime() { var d = new Date(); var hours = d.getHours(); var mins = d.getMinutes(); var secs = d.getSeconds(); document.getElementById("hdnHour").value = hours; document.getElementById("hdnMin").value = mins; document.getElementById("hdnSec").value = secs; document.frmTime.submit(); } </script> </head> <body onload="setClientTime();"> <form name="frmTime" action="serversidescript.php" method="post"> <input type="hidden" name="hdnHour" id="hdnHour" /> <input type="hidden" name="hdnMin" id="hdnMin" /> <input type="hidden" name="hdnSec" id="hdnSec" /> </form> </body> </html> I've changed my mind about 4 or 5 times...I'm just going with whatever I get to work....ha ha I'm not sure I understand how to use this script. It seems to basically redirect to whatever php file is in the action. How do I tell this script to change to the Tuesday include file after 10 pm on Monday night? Forgive my ignorance, I promise I'm smarter than this...ha ha. Maybe my coffee hasn't kicked in yet. Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/#findComment-658917 Share on other sites More sharing options...
R0bb0b Posted October 7, 2008 Share Posted October 7, 2008 I'm not sure as I haven't kept up with the thread, but all you need to do is call this script and it will post back with the client's current time. Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/#findComment-659036 Share on other sites More sharing options...
Twitch Posted October 10, 2008 Author Share Posted October 10, 2008 I appreciate all the help, but nothing seems to work for what I'm needing. I may just scrap the idea. Again, appreciate your time. -Twitch Quote Link to comment https://forums.phpfreaks.com/topic/127326-should-be-simpleor-so-i-thought/#findComment-661943 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.