I'm doing a school project in which I have to put news in an SQL database and, when a certain time passes (around a week or so) automatically edit the database to notify that a specific piece of news is no more active.
For example, I put on a news on monday, and I want it to automatically disappear (or appear as "closed") on the morning after.
So, what I would need is a PHP function (or an SQL one) that automatically starts with a specific timeout and edit the database.
Does anyone know something like that?
Thank you in advance for your help
PS I'm using PHP 5.3.10 and SQL 3.4.10 and I'm stick to them because they're installed on my lab computers
I don't think so. You would use a cron script or something similar to do this on a timer - but this is the wrong approach anyway.
What you need to do is add a time constraint to your SQL. So that your SQL "gets all the news articles for the last 7 days" or whatever
You can do this as follows:
SELECT * FROM news WHERE date > DATE_SUB(NOW(), INTERVAL 1 WEEK)
That way every time a user goes to your page, they will get all the records within a week of the current time. And you don't have to delete anything.
Remember the PHP and SQL query are only run when the website is refreshed. Hope that makes sense.
Related
I have made an employee management system as such, and it calcualtes leave days. Although I am trying to work out how to make the leave days update yearly according to the staffs starting date.
I think I know how I will do this easily. Except it will involve the software being open on that date.
How would I account for each day, without the software having to be open?
Heres the process i was thinking:
-Loop through array of staff data and determine if starting date matches current date.
-If it matches then add an ammount of days to their leave days
I know this is very basic, and it has some flaws. Here are the flaws i am thinking:
-If the page were to be refreshed, or page opened again then it would add x2 of the staff days.
-Also, if the software was not to be opened on this day then the staff would not get the added leave days.
How would I get around this issues? I feel like it should be an easy thing, but I cant think on how to do it properly.
Any help would be greatly appreciated,
Create your php script I'll call it leavetime.php
Then if you have c-panel on your server, there is a thing called cron, in there you can select the time to call it. typically it will be something like this for the command
usr/bin/php -q locationof/leavetime.php
https://www.youtube.com/watch?v=ZAbefcWLxrw
As for the actual php code to do this, it's to broad a topic to cover in a single question.
not sure if this is possible to do but...
i have a follow function on my cms and would like a form of 'trending' posts that would allow me to show the posts that have gained the most followers over the past 7 days or so.
If i have a way of counting the amount of followers for today, is there a way i can check it against a past amount?
Or whats the best way around this? should i possibly be creating a script that will run a query on all the blogs, count there followers and store that in a DB field? I would make this happen every 7 days and run a check against this number and then the current follower amount?
Is this possibly over complicating things?
Thanks, Craig.
It looks like you are headed towards a Cron Job that will run periodically
If i have a way of counting the amount of followers for today, is there a way i can check it against a past amount?
Yes, save your data in a table just for this purpose. Then setup a script that checks this special table in your database where the stats are kept, and chooses the top ones. This table get's updated everyday, say at 00:00. With the data saved, you may query it to find out as much as you're saving in it
TLDR: this is kind of a project in itself, but I'm no expert
I have an idea for a project that I am currently working on, I am however struggling to find a way to implement this in PHP/MySQL.
I have a database with a list of activities that a tattoo parlour has, basically he works from 9 to 5 and when an event is added it is added to the database using date and time
| --- DATE----- | TIME |
2012-11-02 ---| 10:00:00
What I am trying to do is list all the times and dates within the tattooist timetable in the following week that does not already have events going on, this will also ensure the tattooist doesn't double book too!
any assistance will help guys, any assistance will help
The best way to create the application is to store each scheduled event into a database and "black out" dates that are already taken by the applications "events" table (per say)... You will probably want to do your event creation checks during the point that the event would be created or written to the mysql db.... You will probably want to have an events table with a start and end time for each event.... when you go to schedule an event (assuming user input dictates event time and date) just write a mysql query that selects the event where the DATE(a.lastevent) = DATE(NOW())... let me know if you have further questions
Edit:
I would build the front end of the application calendar style using a JavaScript interface. In the long run it will be much more user friendly and much easier to create, I've implemented them in a day.
The framework I suggest using is ExtJS, there are many tutorials and extensions for calendars, I know that ExtJS 3 has a calendar built into it and you can view a tutorial on that here: http://www.sencha.com/learn/using-calendar
Additionally, if you decide to use ExtJS4, you can use this plugin: https://github.com/bmoeskau/Extensible/downloads or this one http://www.sencha.com/forum/showthread.php?142488-Extjs-4-based-open-source-Calendar-application
The second link is open source, the demo looks to be Spanish (it does what you need and more), but if you use Chrome you'll get a bar at the top asking if you want the page translated, click yes, it does a great job. From there you can do find and replaces to get it to English.
The ExtJS 3 tutorial will show you how to interface the calendar with a PHP page so that you can update your MySQL database via AJAX. It is much simpler than you might think, and you'll save a ton of time trying to code it from scratch.
Sometimes it depends on how long each scheduled event might be. Are you adding these events with just a timestamp or are you also tracking how long of a block is being assigned?
you could have an appointment set using a start and stop time and some math..
I have a annoying tendency to use a epoch timestamp instead of a mysql date time. then you can make calculations to see if an appointment overlaps or find the gaps. just have to learn to count in seconds or provide math to adjust :).
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.
So I am really stumped because I have basic ideas but I am looking for some of your expertise.
What I am trying to do: I want to basically write an app using Twilio which you dont really need to know about because that is another issue. What that app does is call on a php file in my web host and "triggers the php code"
What I need help with here is how can I keep record in php of the calendar of the week for my computer. What I mean by that is if someone like an admin has a specific code that I have written for them, and that code runs automatically all week, but a specific week they dont want that code to run, instead they want a different code to run that week. How can I use php to find when a week has ended or keep track of the week using that calendar in bottom right of your computer screen so that my program will know after an admin wants a different code run from the usual code that the week is over no need to run that admin irregular code any more go back to your usual automated running code.
If you still dont know what I am talking. I will try to explain more. Think of 2 separate codes. One Custom and the other automated. The automated runs all the time automatically. But one day the admin chooses for that week he doesnt want to follow the regular shcedule of running the automated code as usual, instead for that week he would like to run the custom code and after the week is over go back to running the automated code as usual.
I hope that makes it more clear. I know that in PHP gives the date. But I really need expert opinion on how to do this.
Generally for something like this, I'd generate a "nextrun_datetime" for each and every script/user combination. By default it would have a repeat interval, in your case, 7 days.
If a user doesn't want to run it this week, they can "push" it out N days and the normal update interval would apply afterwards. To get the one-time shot, I'd allow an update interval of 0 or -1 to denote that.
With this sort of thing, whenever a script is updated (or saved, run, rescheduled), you can calculate the next date if there is one. From there, it's a relatively simple cron job that should check the last N minutes for any scripts to be run.
Unless your client machines are running on a completely different calendar than the server, why bother with wondering what the client's date is? Unless the client and server are in different time zones, the client date is going to be the same as the server date, except for a few hours around midnight.
As well, why depend on the client to trigger the server-side code? If this is a regularly occuring thing, use cron or whatever's available on the server to run the code automatically. If an admin wants to override WHICH code gets run, then you can provide an interface to change what's executed. Click a button on a site and a flag is set somewhere that tells the timed job to run script B instead of script A.
I've done something similar. Based on the day (e.g. monday, sunday) I would do something different in php.
this is how I did it:
$today = date('w');
if ($today== 0){
//its sunday
exec('rmdir C:\myApp\oldLogs');
}
else{
echo '1 -> monday, 2-> tuesday etc...'
}
you can also make a date from a string for example
$date = strtotime("8 days ago 14:00");
/*
or "Monday next week", "+1 week 2 days 4 hours 2 seconds","yesterday noon","10 September 2000" etc...
*/