UnknownPlayer Posted May 11, 2011 Share Posted May 11, 2011 How can i make code which can do something when time is 00:00 ? Is that possible in php, and if not any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/ Share on other sites More sharing options...
Psycho Posted May 11, 2011 Share Posted May 11, 2011 It would be interesting to know what, specifically, you want it to do. But, yes, it is possible. Look running "cron" jobs. There's too much to try to explain in a forum post - especially when there are many resources readily available. Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1214059 Share on other sites More sharing options...
xyph Posted May 11, 2011 Share Posted May 11, 2011 WHEN time is 00:00? Generally, no. IF time is 00:00, yes. PHP isn't a language that waits for events. It can check if an event has happened upon execution though. Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1214082 Share on other sites More sharing options...
Psycho Posted May 11, 2011 Share Posted May 11, 2011 WHEN time is 00:00? Generally, no. IF time is 00:00, yes. PHP isn't a language that waits for events. It can check if an event has happened upon execution though. Which is why there are things such as CRON which can be used WHEN a specific time occurs. Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1214195 Share on other sites More sharing options...
UnknownPlayer Posted May 13, 2011 Author Share Posted May 13, 2011 Then what is solution ? To check if time is > than 00:00 ? Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215221 Share on other sites More sharing options...
markjoe Posted May 13, 2011 Share Posted May 13, 2011 Technically it is not possible IN php. You can schedule a php script to run through the cli process at 00:00 with cron on unix-like systems or with Scheduled Tasks on windows. Either way, we would need a little more info before offering a significant solution. Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215224 Share on other sites More sharing options...
UnknownPlayer Posted May 14, 2011 Author Share Posted May 14, 2011 I want to start voting when it is 00:00, and close when it is 00:00 next day ? Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215335 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 You have the solution: crontabs Now google it. Here's a resource I find myself. http://adminschoice.com/crontab-quick-reference Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215337 Share on other sites More sharing options...
UnknownPlayer Posted May 14, 2011 Author Share Posted May 14, 2011 Can you, or someone give me code how to do that with my example, please? Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215405 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 If you have access to a control panel, then you can set up a crontab. It's very straightforward... especially if you read that tutorial I provided. Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215409 Share on other sites More sharing options...
UnknownPlayer Posted May 14, 2011 Author Share Posted May 14, 2011 I found in cpanel crontabs.. Now i need to setup some file to run when it is 00:00 which will set voting as disabled? Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215415 Share on other sites More sharing options...
Zane Posted May 14, 2011 Share Posted May 14, 2011 all you have to do is write a script that disables voting... then go to cpanel's crontab entry and tell it to run everyday at 00:00. You don't have to put the time in your PHP code. It should look something like this 0 0 * * * disablevoting.php but you don't want disablevoting.php to be available to the public, you might want to put it out of the web root. Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215420 Share on other sites More sharing options...
UnknownPlayer Posted May 15, 2011 Author Share Posted May 15, 2011 I understand now, thanks guys.. I just thought that there is some other solution with php ajax js.. Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215767 Share on other sites More sharing options...
Zane Posted May 15, 2011 Share Posted May 15, 2011 Think of it this way. Without the use of crontabs, it would be no different that having someone go run the same errand for you over and over and do nothing about it until midnight.... and then continue to run the fake errand again until the next midnight. It's really a waste of effort, energy and time. Especially when you have the option, all the while, to send this errand person out ONLY at midnight. Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215771 Share on other sites More sharing options...
r0b Posted May 15, 2011 Share Posted May 15, 2011 I've actually made this happen in plain PHP and HTML. (the code for it is below) It basically checks the current time for the wanted one. So lets day we have a variable named $currentTime which is whatever the time it is. The next variable is $wantedTime, which is, in your case, 00:00. I ran a check if $currentTime equals $wantedTime every time the page refreshes. (I used meta content refresh every 59 seconds, which isn't good, but in my case, it did the justice). When the time equaled one another, it redirected to another page. EDIT: (heres the code) - Save this as index.php and change the anotherPage.php to your wanted page with execution. Or you can execute it on the first if statement. <?php $wantedTime = "00:00"; $currentTime = date("H:i"); echo "The time is: $currentTime<br />"; echo "Script will execute at $wantedTime<br />"; if($currentTime == $wantedTime) { echo '<meta http-equiv="refresh" content="0; url=anotherPage.php">'; } else { echo '<meta http-equiv="refresh" content="50;url=index.php">'; } $to = strtotime($wantedTime); $from = strtotime($currentTime); $calculation = round(abs($to - $from) / 60,2); echo "<br />The code will execute in $calculation minutes"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215778 Share on other sites More sharing options...
Zane Posted May 15, 2011 Share Posted May 15, 2011 I've actually made this happen in plain PHP and HTML. What it did was, to check the time for the specified one. So lets day we have a variable named $currentTime which is whatever the time it is. The next variable is $otherTime, which is, in your case, 00:00. I ran a check if $currentTime equals $otherTime every time the page refreshes. (I used meta content refresh every 55 seconds, which isn't good, but in my case, it did the justice). When the time equaled one another, it redirected to another page. Yes, but that method will only work if your page is visited everyday... and especially at midnight. Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215781 Share on other sites More sharing options...
r0b Posted May 15, 2011 Share Posted May 15, 2011 I've actually made this happen in plain PHP and HTML. (the code for it is below) It basically checks the current time for the wanted one. So lets day we have a variable named $currentTime which is whatever the time it is. The next variable is $wantedTime, which is, in your case, 00:00. I ran a check if $currentTime equals $wantedTime every time the page refreshes. (I used meta content refresh every 59 seconds, which isn't good, but in my case, it did the justice). When the time equaled one another, it redirected to another page. EDIT: (heres the code) - Save this as index.php and change the anotherPage.php to your wanted page with execution. Or you can execute it on the first if statement. <?php $wantedTime = "00:00"; $currentTime = date("H:i"); echo "The time is: $currentTime<br />"; echo "Script will execute at $wantedTime<br />"; if($currentTime == $wantedTime) { echo '<meta http-equiv="refresh" content="0; url=anotherPage.php">'; } else { echo '<meta http-equiv="refresh" content="50;url=index.php">'; } $to = strtotime($wantedTime); $from = strtotime($currentTime); $calculation = round(abs($to - $from) / 60,2); echo "<br />The code will execute in $calculation minutes"; ?> NOTE: In this code I used a 24 hour system while you probably need a 12 hour one. If thats the case, replace the capital H to a small h. Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215792 Share on other sites More sharing options...
r0b Posted May 15, 2011 Share Posted May 15, 2011 I've actually made this happen in plain PHP and HTML. What it did was, to check the time for the specified one. So lets day we have a variable named $currentTime which is whatever the time it is. The next variable is $otherTime, which is, in your case, 00:00. I ran a check if $currentTime equals $otherTime every time the page refreshes. (I used meta content refresh every 55 seconds, which isn't good, but in my case, it did the justice). When the time equaled one another, it redirected to another page. Yes, but that method will only work if your page is visited everyday... and especially at midnight. Sorry for the double post. Correct, the page has to be opened by atleast one user, in my case I had this page opened at all time at atleast one computer. Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215796 Share on other sites More sharing options...
Zane Posted May 15, 2011 Share Posted May 15, 2011 Keeping it open will not do any good. The only way it would do any good is if you refreshed the page every minute.. on the minute. Which you can easily script it to do, but it would probably slow your server down quite a bit... possibly to where you need to restart apache. Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215801 Share on other sites More sharing options...
r0b Posted May 15, 2011 Share Posted May 15, 2011 Keeping it open will not do any good. The only way it would do any good is if you refreshed the page every minute.. on the minute. Which you can easily script it to do, but it would probably slow your server down quite a bit... possibly to where you need to restart apache. Its exactly what my script posted above does. It refreshes the page every minute and checks if the value is a match yet. One refresh per minute equals a 1440 times refreshed page per day, which would equal 1440 visitors per day. I hope apache wouldn't need to be restarted for that small amount of hits. (I made it refresh every 50 seconds, so that would make it refresh 1200 times, which is even less than 1440.) Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1215805 Share on other sites More sharing options...
UnknownPlayer Posted May 16, 2011 Author Share Posted May 16, 2011 This code is fine for countdown, but can you make to have some js and refresh only timer, i mean to decrese minutes with echo, without refreshing page ? Quote Link to comment https://forums.phpfreaks.com/topic/236136-help-with-realtime-php-things/#findComment-1216069 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.