How can I time something in PHP? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to do on a script I`m working on that every month a part of the site resets.
Or a better example, Something like a Subscription, when you want to buy something and you need to renew it every month. How can I know its been a month?

In PHP you can't do regular cron jobs, and I will discourage you seriously from doing it with real cron jobs if you don't know what you're doing.
You can only register when you last executed that event and then check if it's been a month since then. This is a really simple sample cron:
<?php
$lastexecution = /*logic to know when you last executed.*/;
/* It's either a database or a file or something similar.
* I usually use a database table that contains the records when I
* last executed a cron
*/
if (time() > $lastexecution + (30 * 24 * 3600)) {
/*CRON LOGIC*/
}
You also should look into flock() or some similar locking mechanism to prevent the cron being triggered by two different users simultaneously.
Note: In your case, with a subscription, you could add a expires field to your database that would contain the date and time when the user's subscription needs to be renewed. If that date is in the past, you tell them

Related

Autoupdate every day on database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm having local MAMP server with phpmyadmin with three tables, users, booking and rooms. In table booking I have something like bookingFrom bookingTo columns that represents booking time. In rooms table I have something like isBooked aswell which is depending on time mostly.
I'd like to make something in my phpmyadmin that every day database will update itself and check if the room is occupied and if the bookingTo date will be before the day that currently is the database will automatically change isBooked from true to false.
My goal isn't the ready script to get, but the way to do it or any useful links as long as it is my training website for school project :)
you can't do such as think with phpmyadmin. You can try mysql scheduler if your version supports it.
Something like that should work...
CREATE EVENT myevent
ON SCHEDULE EVERY 1 DAY
DO
CALL check_book_status();
Please don't forget to create check_book_status function that updates field that you need.
Also, here more information...
http://dev.mysql.com/doc/refman/5.7/en/create-event.html
It might be possible to do this in the database directly, I'm not aware of any method that does this, but I'm sure it's possible (maybe not in mysql, but in some db-engine). However, can't you just do a simple PHP-run of an UPDATE-query at the start of the day (or, whenever), or even just run a cron-script at a specific time (half an hour after checkout, or something like that) and have it check the "end-date" in the database, and if it's before the current date/time, change the "occupied"-status?
You can make it without update status. Suffice calculate status when you show this info:
SELECT
IF(NOW() > bookingFrom AND NOW() < bookingTo, 1, 0) AS isBooked
/* other fields */
FROM booking
...

Background update in database using php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We have a database table with created datetime and status flag. We would like to update the status flag to the next status if the created date time has elapsed by 30 minutes without any user intervention. How can we achieve this in php.
Create a php file that does this status change in the database and program to execute it every 30 minutes with cron (Linux) or Task Scheduler (Windows).
You have to think carefully about how you design and use your database. Sometimes things are made overly complicated when they don't need to. For instance, in this case you could use a 'datetime' in your table indicating the start time. Any PHP script can now check whether or not the start time started 30 minutes ago, only when this information is actually needed. No need for a flag, cron jobs, etc.

What is correct for auto schedulling of a job using PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have an application written in PHP and MySQL. There I need to activate calendar on a specific and again deactivate that calendar on other specified date. This process will occurs once in a year gap between 2 dates is approx 1.5 months.
how should I do this in PHP? Should I use cron jobs?
Having your requirements, I think it would be more stable to add the check "should I show the calendar" into the source code itself. When the check is well written, it should not consume measurable response time.
Having the cron job, you always will have to look at an additional thing. This could be forgotton or whatever. Have experienced that too often ;)
Example for a code that checks that:
$now = new DateTime();
if($now >= new DateTime('14 Mar') && $now < new DateTime('15 May')) {
show_calendar();
}
I would go with the approach hek2mgl has suggested.
or, if you want to set up a cron job then here is the url you should be using to execute your script on given time:
/usr/bin/php5 /your/directory/path/cron_script.php
To check the path:
Create an index.php in your root folder.
<?php
var_dump(dirname(__FILE__));
?>
Hope this help.

Creating subscription for website [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I would like some guidance in this. I am creating an update for a website that will allow a customer to subscribe to buy ebooks and have them sent out each month and automatically bill. I know how to use a sql database and PHP to keep track of members etc, but how do I make my website so that it will schedule things automatically at certain intervals. I tried Google searching, but maybe I didn't know how to phrase it.
With the help of Cron Job, on Linux hosts. Cron job can be used to automate your php script. So you write a script that does what you want and schedule it with Cron. Best approach is to write in the table when do you want Cron to run. For instance:
You have a column named "SendBook" with the date when the book should be sent. You set your php script to check if today is that date and if it is, send the book. For more help, visit this site
Code example:
$sevendays = date('Y-m-d', strtotime("+7 days")); // date is set to seven days from now
$sql= "INSERT INTO tablename (SendBook) VALUES ('$sevendays')"; // date is passed to the table
mysql_query($sql);
Than you create php script that checks if today is that date:
$checkdate = date("Y-m-d");
$sql = "SELECT SendBook AS '$comparedate' FROM tablename";
mysql_query($sql);
if ($checkdate == $comparedate){
// send email or whatever you want to do
}
Than simply make second script run whenever you want with Cron

PHP Auction Expiry Tracking [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What is the best way to send e-mails or perform functions on a small, non-real money auction script? This script is a learning exercise for me and I was wondering what the best way would be to process actions when the auction has expired. A cron job every minute seems - to me - a method which can easily be surpassed.
To be honest, this is an ideal situation for a cronjob. Theoretically, you could create cronjobs on the fly...That is edit the crontab with php and create an entry for each auction with their end time to execute a generic script that has some variables passed to it.
A cronjob every minute seems a bit extreme, but if you space it out a bit the idea seems very reasonable.
Alternatives would be for if someone hits the auction page that the script checks to see if the auction is expired and then sends an email and sets a flag in a table column. This overall is a terrible idea (What if no one visits the page for a few days).
Ideally the cronjob is your best friend here. Now you can go with an hourly style cronjob or create a script that generates one time cronjobs on the fly. The best solution though would be a recurring cronjob (per 10 minutes perhaps?) and as it sends out an email for an auction, a flag is set in an column ie email_sent to 1 or something similar so that emails aren't resent erroneously.

Categories