Laravel automatically delete blog post when it pass its expiry date - php

In my Laravel app I upload blog posts. All posts have a unix expiry date eg 1492425121 What I want to do is to automatically delete all posts that pass its expiry date.
Right now I am running a cron job each night that checks for old posts then deletes them, but is there any other way I can delete them right away?

You can use scheduled task for that. In this task, do something like this:
Post::where('expiration_date', '<', Carbon::now())->delete();

Then instead of running your command on night make it schedule that it run every minute and check whether post exp time and server time equals then delete it ,
schedule your command like this way:
$schedule->command('MakeXpZero')->everyMinute();

Related

How to execute some code on a specific datetime to the second?

I'm building a market place with auctions. I need to end auctions preferably at the exact second they are defined to end according to their record in the mysql database.
I know about CRON jobs and I'm currently running a CRON job every minute to execute some script which checks if there are sales that should end.
However this sometimes means that an auction ends almost a minute too late, which is not acceptable. Does anybody know how to make this more accurate without putting a lot of load on the server? For example I wouldn't want to run a script every second to check if sales have ended.
Check timestamp everytime a user try to click auctions button. So even the page haven't refresh and times up, user is not able to bid your item.

Automatically update data on website database triggered by date and time

I have a condition that I have to update data automatically on my website, which is triggered by time that I've set before.
EXAMPLE :
i've set the update for 29-05-2018 20.20 then the website will do update in that date and that time without any trigger, and no need to
open the website.
Create a Controller name Cron.php
than make a function run_every_day()
now put all your condition in this function than go to your cpanel search for a cron tab. Click on it create a cron task
give it the url root/index.php/cron/run_every_day
in the cron seeting set the time 1 day so it will run each day on specific time you set.
Hope this helps

How to run a PHP script automatically in every month

I have the php project for user can register and buy the hours to listen music or video, the not used of 50% hours have to rollover to next month, I wanted to automate this process when user complete one month I have to check their usage and process the rollover using php. I have following user details on DB, Signed-up date with time and usage and hours they purchased and used hours. I want to do it once in a month for each customer.
Is it possible using cron jobs? I don't have any idea to do this.
Not sure if this is the right approach but I've been running such kind of scripts when a user logs into his account.
For example.
You can do something like on successful login request by the user
if(date of buying hours has crossed the specified limit) /here you can check for expiry date/
{ // run your script here
}
else{}
just a suggestion!
You don't need to update all of the records all at once. Just update it for the user who logs in.
At the end, it will be updated for every user I think.
yes this possible with Cron Job 1st Create your php file with your condition use update query then add this cron job file in your server Cron Job Section and select date when you want to run this file. in cron job just file your cronjobfile.php name , Date and time when this file run.
Short answer: Use Cron job.
Syntax and usage of how to use it is almost straight forward. Here's a cheat sheet.
Note: If you're on a shared host, some hosts don't allow you to use cron job.
Edit: What #Havenard suggested is correct. The below cron job will visit the url once at minute 00 of every hour:
0 * * * * wget http://www.mywebsite.com/some/link

Wordpress post only visible for a set period of time

Does anyone know if it's possible to make a wordpress post active for a set period time e.g. 2days and 6hours ?
Maybe we need to use custom field for second date or wp schedule event, cron Linux to delete/hide post ?
You can try to use this plugin:
https://wordpress.org/plugins/post-expirator/
It allows you to set an expire date along with what to do (e.g. set post to draft, delete post).
The plugin hooks into the wp cron processes and runs every minute by default, but can be configured to use any cron schedule (hourly, twicedaily, daily, etc).

Executing a script at an exact time in future

I work on a PHP project where users place advertising posts. When advertising post is placed online it is given a "hide date" (stored in database), when it will no longer be shown to the users. When "hide date" (e.g. next Monday at 10:00am) occurres I need to fire a script that makes changes in the database (not related to hiding the ad).
Question:
How do I run a script at the exact time in future(at "hide date").
I google'd a lot and didn't find any good solution.
Found possible solutions:
Cron isn't considered any more because of its lack of accuracy (for my task even seconds matter).
Write a daemon which will check every second if there is a record with a "hide date" but it can be very consuming.
Another way as I was told that message queues could be of help, but after reading some of the documentation I didn't find a way of setting date and time of processing the message in queue.
Can anyone point out where to look? Any tool/language is considered.
Let's suppose that ads show until a week after
Create a field in your ads table called expiry_date (timestamp)
When a new ad is inserted, do the following
$expiry_date = strtotime("+ 7 days");
Then insert the expiry date along with the record
Next, in the page that displays the ads, select only ads whose expiry date did not pass yet, so
SELECT * FROM ads WHERE expiry_date >= NOW()
This will only show ads that didn't expire yet.
If your site is running on a linux server, you can add a cron job to trigger your update script.
If your site is running Windows, use Task Scheduler instead.
However, it doesn't really sound like your best solution. You could store the "hide date" in the database along with the rest of the post's data. Then you can simply filter the posts in your display code.

Categories