How can I decrement a MySQL field every day?
Such as:
field1= 30 and after 24hours -> field1=29 until 0.
You'd probably be better off placing the current date in the database, and then use DATEDIFF to get the days since. You could even do 30 - DATEDIFF and then have the number the way you want it.
You can execute a Unix cron job (there's some informations here) which will execute your PHP script every day.
Related
I have a MySQL database containing details of shops in different time zones. The timezone of each store is stored in iana format and the MySQL datetimes are stored in UTC.
I wish to execute some php code at the end of the day for each shop.
This is how I am thinking to approach this but is there a better way?
Set a cron to run hourly at xx:59:59.
Get the current date at the top of the php script. Is the script guaranteed to get the correct date at 23:59:59?
Use SQL query to return all shops that are at the end of the current day. I'm not sure how to check this in the query?
Perform the end of the day processing on those stores.
You can run an hourly cron job any time you wish.
Why do you need the date here? You wrote that you're interested in the servers, which are at 23:59:59 localtime, so time is relevant, I think, not the date. Also, if your details server is very busy, your script might run too late and get the next day's date.
If IANA time zone format means offset to UTC, you could simply look for shops having a timezone like
24 - current time(UTC) +/- DST
Negative timezone offsets work similar, e.g. 24 + offset. So timezone offset -01:00 would become +23:00
Nothing to say here.
I need to set the current date and time in static variable.
I need to insert the 50 records into database table. Here,I need to insert the current date and time. Then, I need to set the current date and time of the 50 records are same. I used this date('Y-m-d H:i:s'); format. This format will change every minutes and seconds.
How to I do. Please help me.
$date = date('Y-m-d H:i:s')
then use this variable,for create records. All records will have same time.
It's been a long time, but I found a simple solution. Maybe it's useful for someone (like me) that wants to do the same thing nowadays.
Even when you store the current time/datetime to a variable, the time it's still running, so it changes every second.
I solved it by storing the time() value into a MySQL table (datetime type of course), so the captured time will be stored as it is and stop running and changing every second. Then, when I want to use it, I just make a query from the MySQL table.
It's a simple way (for me) to capture the "now" value and make it stop running, but maybe there's a better way.
I have a table called profile_views on my site that counts how many times a person views a users profile. What i am trying to do is let users see how many visiters they've had to their profile in that week, and every week I on Monday I want the table to be emptied so the count can restart from 0.
I am trying to create a php if statement that says every week on monday carry out the delete mysql query. I have got it to perform only if the day is monday, however I am trying to find a way to execute it only once, preferably at 00:00AM monday morning, because I dont want it deleting results in the table constantly throughout the whole of monday.
<?php
$today=date(l); // Find what today is? using date function
// If today is Monday displays message "Today is Monday" and displays image1.gif
if($today==Tuesday) { // Compare $today with name of the day.
$result = mysql_query("DELETE FROM ptb_profile_views;")
or die(mysql_error());
}
?>
What you are looking for is named cron - use it to invoke your PHP script on desired weekday at desired time.
Setup Cron - based on our requirement
http://www.sophos.com/en-us/support/knowledgebase/12176.aspx
http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
you could set up a cron job to automatically execute the script, but a better way (the way I do it) is to just leave the entries in the database and when displaying the number of visitors in the last week, change your query to return only entries within the last week. this might be much easier.
A method without cron jobs:
You can set a variable $ran and store it somewhere (maybe in a file?). Then every Monday, run and store the $ran into the file as true.
If it's any other day, set $ran back to false. This will ensure that the script will only run on Monday, and only run once.
I am trying to set up a database to record the last 30 days of information for each user. The data will be recorded once a day (i.e. by a cron job) and will be the value of an item (i.e. constantly changes).
What would be the best way to structure this? I was thinking of setting a table and then just storing the 30 days in the table and deleting the 31st day as I add the new day with the cron job (and shifting all of the others up one day) but this doesn't seem very efficient..
Thanks for the help.
What you can do is store the current date with each entry, then in your cron job, delete all entries that are greater than thirty days old.
For example (with MySQL),
DELETE FROM user_statistics WHERE DATEDIFF(NOW(), date_of_record) > 30;
Store the user data with its own date and delete the oldest when you exceed your limit. No need to shift anything.
I'd log by actual date using a DATE column. You can query up "last 30 days" pretty easily in MySQL.
As for purging, the cron job can delete anything older than 30 days pretty easily as well. Or, since it's so easy to ignore anything older than 30 days, you might even choose to not delete older records (at least not every day).
I'm trying to create a computer reservation system, where user chooses a computer and select the time how long he will be using this PC. In that time other persons can't reserve this pc, I need to find a solution, how to automaticaly delete all rows containing reserved pc's after their time expires. Thank you for the advice.
The common way to handle this is to store an expires_at timestamp on the reservation row. Then your query to find any "open" reservations would have WHERE 'expires_at' < NOW() or something similar.
This is an untested answer, that may only be a suggestion, but I just started looking at these, so am interested in feedback as well. i'm still working through possibilities and drawbacks, but it might well suit your need.
Take a look at MySQL Events, an article about it is here, and official syntax at Mysql Docs.
Per the article:
An event is similar to a trigger. However, rather than running in
response to a data change, events can be scheduled to run any number
of times during a specific period. In effect, it’s a database-only
cron job.
Pondering this, I'd envision a procedure that deleted anything >1hr (if that's the expiration). This procedure would be TRIGGERED on new inserts to get rid of anything expired at that moment, but also in an event to run every 15 minutes or so so that automatic deletes by the trigger aren't dependant on somebody else adding a reservation to trigger that procedure.
If your server is linux, you can use cron jobs to check once a day every reservation dates. If these dates have expired .. modified field reserves to be available.
Normally I would do it this way:
when storing a reservation, store date_from and date_to both of datatype DATETIME
when checking if there is a computer free check for all computers and filter with WHERE '{$my_date}' >= date_to AND '{$my_date}' <= date_from - by this You should be able to get all the PCs that are not reserved within a certain time...
To be complete in the solution, you need to run a CRON job which calls a query to remove all reservations that have a reservation_time + (15 * 60) < unix_timestamp().
I am assuming you have a time that the reservation was placed or started and are using UNIX/Epoch Timestamps.
Instead of doing a expires_now, if you know it will always be a fixed interval ie 15 minutes, you can do:
DELETE FROM reservations WHERE reservation_time + (15 * 60) < unix_timestamp()
Something you could look into is managing cron job's from PHP, http://www.highonphp.com/cron-job-manager.
The above script will, when a reservation is created, insert an entry into /etc/cron.d/ and you could configure it to run at the expected reservation endtime. Then inside the php file which would be executed, you could do:
DELETE FROM reservations WHERE id = :id