Executing a query in a specific hour - php

I have script that run a php every minute. I want to make a variable that store the current hour everytime its runs so i can make an if() statement so when its 7pm execute the code inside.
$hour = date('H');
If($hour >= 7:00pm <= 5:00am)
{Do something that i know how to do it xD}
Please no Cron Job or similar.

date('H'); will return the current hour.
You can then use it in an if-statement.
It is hard to provide more than this as you showed very little of how and why you want to perform this check.

#Miguel
By default, PHP took the time from server where its located. You can set the default time in your common PHP file(which includes in all pages or at-least in that page, in which you wrote the code to execute).
using the below function, you can set the default time zone for your web application.
date_default_timezone_set('America/Los_Angeles');
here is the list of Timezones (http://php.net/manual/en/timezones.php), select according to your desire country/city and pass that value in the above function.

Sounds like this might be handled better via a cron. Just a thought.

You have several options, something rather alternative that might be a better solution depending on what you are trying to do, is to just setup the script to run everyday at 7PM.
This can be done via cron jobs in linux with the following command:
0 18 * * * php -q /path/to/php/script.php
This will be based on your server time and timezone. Based on your comment on Emz's post, You should know that when you track the time with php you need to realize that php.ini can change the time in php scripts as it has it's own timezone!

Related

Automatized action on scheduled date in MySQL

I need an action to be realized automatically when a specific date is reached in a MySQL database, so not necessarly when a user is logged in the site and does an action.
This would go for many different entries in the table.
For example, when UTC_TIMESTAMP() reaches 'release_date' for different items, a PHP script would be executed, respectively for each items.
This is something I have never approached before.
I'm reading about Cron Job and MySQL EVENTS. Would that be the way to go?
Thanks in advance! Any help is greatly appreciated.
Lois
The best way to do this is to have a cron job that runs every time interval, you have full control of how often the script will run,
Within this script, you'll basically check the current time and see if it hits your 'release_date', then you run some queries against your db, if not, the script just exits.
so you can create a cron entry that runs every hour, like this
00 * * * * /usr/local/bin/php /path/to/script.php
and inside script.php
#!/usr/bin/php
$release_date = 1383135371;
$now = time();
if($now >= $release_date){
//Connect to database and do your code here
}
This way you'll have much more control of how things will work, this is not even scratching the surface of the powers of cron jobs !, let us know if things aren't clear :)
What you can do is create a PHP script that would run your query, do your comparisons, and then run the appropriate scripts. You would then run this script from the command line via cron. Here's a page that describes it in more detail
http://www.thegeekstuff.com/2011/07/php-cron-job/

Posting data from database at regular time intervals automatically in php

I have questions stored in my database. I want to regularly post one question on my website from the database at a 24 hr interval automatically. Is there a way I can do that ?
You can do this with steps:
Create normal PHP-script which will post your questions.
Schedule your script with standard OS scheduler. It is cron for *nix (Win-versions exist too) or AT for Windows. To define certain interval - you should read scheduler's manual (for cron format is provided here)
Example (cron)
0 2 * * * /usr/bin/php /path/to/insert/script.php
-in this case every day at 02:00 AM cron will try to execute command /usr/bin/php /path/to/insert/script.php - i.e. if your script.php will extract your question from DB and post it - that will do the stuff.
Yes you can do it by using Cron job . Set time interval and your file script location. It will automatically hit your script on that time interval.
Here is good tutorial : http://docs.phplist.com/SetupCronJob.html
Providing you could create a PHP script to select a different question each time, all you'd need to do would be to set up a cron to run the PHP script every 24 hours. You can find more info on cron here.
You should look into MySQL date functions.
A contrived example would be using CURDATE():
SELECT * FROM questions WHERE publish_date = CURDATE()
Storing the publish_date will mean you can dynamically load the question when that date arrives.
The only way to do it correctly is to use cron jobs. You should take a look at the administration panel of your hosting service.
Write a script that will post one question on your website everyday and set a cron job to run that script once a day and you are done.
How to set a cron job , ask you hosting service provider , most of the hosts have this feature in cpanel
Yes, you can. I will shortly outline the two most common solutions. The difficulty rises that PHP is not an always running program, but is a language executed on request and then shutdown on completion.
Have some sort of init.php file on your webserver which is being included on every page. That script will check whether the time has passed since last question, and push a new question.
On the other hand, you can add a cronjob which will execute your php script pushing the question. This solution is more robust, but requires access to a webserver you might not have.
Create a php file put the code to fetch question form your database
then set cronjob to excecute the file on perticular time or also
you can execute file by including it your login or any other page which
lods first by including that php file file so that when first user logs in
it will execute.
Steps
Create a PHP script to select and post a particular question randomly.
In your main php script write an AJAX method(which will load the PHP script) which can be called using setInterval() using the following syntax-
setInterval("AJAX_fun()", 24*3600*1000);
This statement will call the AJAX function in a periodic interval of 24hrs. For that you must know AJAX. I mean what should be the body of the AJAX to load the PHP script that you must have an idea of.
Another alternative
You can simply reload the page using javascript setInterval() function
i.e. <script>setInterval("window.location.reload()", 24*3600*1000);</script> and before that you have to select a question from the database randomly using a PHP logic.

run cronjob only once on time defined by timestamps

I would like to execute a PHP-script automatically on a timestamp defined time.
So lets say I have calculated a future-timestamp (e.g. 1359504000) in another PHP-script and exactly at this time (something like) a cronjob should call the other script. afterwards it (I am thinking about conjob currently) should be disabled/killed/deleted.
What would be the best way to do this type of task?
thanks!
If you are going to do this process multiple times, and you don't need strict timing, you can create a cronjob that goes every 30/10/5/1 minutes (depending on your thoughts) which looks for the timestamps and calls the scripts.
I think that there isn't a way to do that.
At the most you can run automatically the cronjob every minute and check if you have to do some function. This because the cronjob is for recuring tasks only

How to delete mysql row after time passes?

I have no idea where to start with this one:
I have a database that stores postID and Date.
What I want to do is have my website auto delete all rows where Date is less than today. This script can't have any user input at all. No button clicks, nothing. The script must run every day at midnight.
I've been looking all over the place for something that does this and I've found absolutely nothing.
You can use PHP script and use cron job on your cpanel.
Example:
cronjobcommand.php
<?php
include 'your_db_connection';
mysql_query("DELETE FROM your_table_name WHERE Date < NOW()");
?>
I have attached a screenshot below for your more reference.
For those out there who are on a shared hosting, like 1and1's, and can't use cron, here are 2 alternatives :
mysql events enable you to place a time trigger on mysql, which will execute when you'll want, without having to be fired by any kind of user input
if you cannot create mysql events because you're on 1and1 :(, an alternative is to use webcron
You just need to tell webcron the url of the php script you'd like to be run, and they'll trigger it for you at the intervals you want
Why using cronjobs everyday?? Why not filter data on output. For example in your select check if post date equals today with adding a simple where:
SELECT * FROM `posts`
WHERE (DATE(`post_date`) = DATE(NOW()));
This way you're not required to do your database managements/cronjobs on any special time and it will be used just for database managements. Afterwards you can delete unnecessary data at any time using by mysql command like:
DELETE FROM `posts` WHERE (
DATE(`post_date`) < DATE(NOW())
)
Most hosts provide a cron(8) service that can execute commands at specific times. You use the crontab(1) program to manage the crontab(5) file the describes when to run which commands.
There's a lot of functionality available to you, but if you write a program (shell script, php script, C program, whatever) that runs the appropriate MySQL commands, you can call the program via cron(8) in an entirely hands-off fashion.
Run crontab -e to edit your current crontab(5) file. If none exists, hopefully you'll get one with a helpful header. If not, copy this:
# m h dom mon dow command
The columns indicate the minute, hour, day of month, month, and day of week to execute commands. All the numbers in the columns are essentially ANDed together to decide when to run commands.
Thus, midnight every night would look like this:
0 0 * * * /path/to/executable
It's remarkably flexible, so put some time into the documentation, and you'll find many uses for it.
You should set cron job (scheduled tack.) for it.
A cron job is an automated program setup for Linux and Unix operating systems. It allows the user to execute several commands or functions at a specific time and date.
you have cron Job in your cpanel setup. first you need to make a php script with your logic for delete record after each date. take date from server and write script for delete.
then go to cron tab in your cpanel and do settings for time interval to run cron and give path of your php script file.
MySQL doesn't have a task scheduler. So you have to use the task scheduler of your Operating System (CRON under Linux), or to lunch a basic task checker sub-script during the script of the main page (on another page that is supposed to display the changing data).

Run a MySQL query if a week has passed

First of all I am a beginner, and I don't want anybody to write code for me. I would just like a bit of a hint from a more experienced developer.
I have a video site, what loads videos from another website with XML and saves info about the videos in the database. What I would like to do is that if a week is passed, automatically run the insert query.
I never did this before and never worked with time functions like this. So please could someone show his plan how he would do it? So no code, just explain the process.
I'd recommend setting up a cron: http://en.wikipedia.org/wiki/Cron
I dont think this is a coding-related problem. Tasking can be achieved by using cron.
Cron is a task scheduler, which when its available for your hosting, can be accessed at the hosting control panel. What is your host ?
You could use Cronjobs.
What do you want to do with the data in the week in between? I hope you're not hoping to keep a process running for a week and then execute the insert.
You could do something like load the XML and save it in the database, setting an active column to 0. You save the timestamp at the moment the insert is executed.
Meanwhile, using cron, you let a script run every X minutes or hours, checking the database for items that have been inactive for a week, and then updating them to become active.
You can use the time() function. It returns the number of seconds since January, 1st, 1970.
Then, for example, you take the time at t = 0.
When time() - t > a week (= 3600 * 24 * 7 seconds), you know a week has passed by.

Categories