In my folder cache, i have dozens of files whose name is filename-number.json, like
sifriugh-80.json
dlifjbhvzique-76.json
dfhgzeiuy-12.json
...
I have a simple script that cleans my cache dir every 2 hours, deleting files older than 2 hours:
$fileSystemIterator = new FilesystemIterator('cache');
$now = time();
foreach ($fileSystemIterator as $file) {
if ($now - $file->getCTime() >= 3 * 3600) // 2 hours
unlink('cache/' . $file->getFilename());
}
Now, i'm looking to only delete every 2 hours files whose number (before .json but not if number is present at the beginning of the file) does NOT end by -100.json, and those ending by -100.json, every 7 days only.
I know that i can use preg_match() to get names, but is there any effective way to perform it?
There is much simpler way than regex using PHP 8+ str_ends_with() : https://www.php.net/manual/en/function.str-ends-with.php
if (str_ends_with($file->getFilename(), '100.json)) {
// unlink at needed time
} else {
// unlink at needed time
}
For PHP 7, there are several ways to emulate it, check at the bottom of https://www.php.net/manual/en/function.str-ends-with.php
I do not see why you should not use preg_match. It is so good here.
Before using preg_match you should check the first symbol in file name and skip the next code if it is_numeric.
If it is not numeric you need to get only ending "-" can be presented zero or ones + number 1 or more digits + .json ending
(pattern can be something like this /-?\d+\.json$/)
After that you can parse it and know the real number before .json,
Your verification will be something like
If "-" symbol exists and number is 100 check the date for 7 days
If number is not 100 check the date for 2 hours
I've researched this a lot but can't find a satisfactory answer; how do I create a PHP script that will generate a new number each day? Obviously I'm using this for a reason other than to generate a random number daily, but I won't go into that reason, it'll just make this question more complicated. So I'm asking this: How do I generate a random number which will change each day in PHP? Using MySQL will be no problem, but it must be automatic so I won't have to manually change it daily. (Here's my 'script' to generate a random number)
<?php
echo rand(1,100)
?>
Any answers are appreciated, Thanks
- Hugh
Use time() function to generate seed, then use regular rand.
This way you should't to store it anywhere and you always can regenerate it when needed.
function randomEveryDay()
{
$now = time();
$today = $now - ($now % 86400); //86400 = 1 day in seconds
srand($today);
return rand();
}
Or more interesting example without random at all.
function randomEveryDay() {
$now = time();
$today = $now - ($now % 86400);
$hash = sha1('salt string'.$today);
return intval('0x' . substr($hash, 6, 8));
}
Every day you will get the same number in $today, then use any cryptographic\non cryptographic hash function to generate "random".
This is fairly a simple task as long as you understand the basics of crontabs.
Step One: Create the script. This is basically going to be what creates the "text" then inputs it into the database via mysqli. For example, if we are generating a random number, what you have so far is good, you will just need to insert it into a database table. I recommend using a time stamp to give what day it was generated on
Step Two: Create a cronjob. Use the servers crontab to run a task every day, this can be done by adding this to the cron file: This will run a cron each new day.
00 01 * * * php path/to/your/generate.php
Step Three Fetch result from database by using the current date. If you are needing to display that text, pull it from the database using whatever the current day is from date() or DateTime
It's impossible to give a good answer without knowing exactly what you want to do, so this will generate a new number every day:
echo date('Ymd');
sorry I am new to PHP and need some help/guidance on creating a counter that will work server side, so I guess update an initial value?
I need for example to start with a base number of 1500 and have that number increase by 1 every 2 minutes, obviously so any visitors will see an increased number each time the visit.
Would the initial value need to be stored in sql or can a txt file be updated?
Any help would be great,
Thanks
It can be done in SQL if you want it but a text file is OK too, just save a value (1500), then create a cronjob and let it execute a PHP file where you'll have to set up the code that executes an SQL query which updates that value OR the code to update that text file every 2 minutes.
Example:
# Every two minutes
*/2 * * * * /your/path/too/this/file/updatecode.php
In your PHP file:
$SQL = "UPDATE table SET columnname = columname + 1";
// etc...
// OR the text file update code
If you don't need to store it specifically for some reason, then you don't need to run cron etc... Take a time stamp of a specific point in time you want to start at. Then calculate minutes since and add it to your start number (1500)
//Start Number
$n = 1500;
$cur_time = time();
$orig_time = strtotime("2013-10-21 10:00:00");
//New Number + difference in minutes (120 seconds for 2 mins) since start time
$newn = $n + round(abs($cur_time - $orig_time) / 120,0);
// Output New Number
echo $newn;
And if you wanted it in one line for copy/paste
echo 1500 + round(abs(time() - strtotime("2013-10-21 10:00:00")) / 120,0);
You could do this without a database just using dates. work out the difference in time between two dates (the current date and the starting date when you created the script), then divide that down into the correct amount of milliseconds for 2 minutes, and add that to your initial 1500.
If storing it is needed a SQL database for this is probably overkill.
Create you number, serialize it and store it to a file. Load it from the file next time, unserialize, increment, serialize and save.
You can save a time stamp along with the number to the file to avoid having to run some job every 2 minutes and instead calculate the correct value when you load the number back from the file.
Something like this (but error checking etc should be added and I haven't actually tried it to make sure the calculation is correct... but the main idea should be visible).
<?php
if(file_exists('mydatafile')) {
$data = unserialize(file_get_contents('mydatafile'));
// Calculate correct value based on time stamp
$data['number'] += round((time() - $data['timestamp']) / 120);
}
else {
// Init the number
$data["number"] = 1500;
}
// Print it if you need to here
// Update time stamp
$data["timestamp"] = time();
// Serialize and save data
file_put_contents('mydatafile', serialize($data)));
All,
I have the following code to figure out a time based on milliseconds that were provided:
$ms = $value['trackTimeMillis'];
$track_time = floor($ms/60000).':'.floor(($ms%60000)/1000);
The issue is that sometimes this doesn't work that well. For example, if I have the milliseconds as 246995 this will output 4:6.
Is there a way to always make it so that it converts this correct and if it does round to an even number to add a zero at the end of it? So something like 2:3 would read 2:30?
Thanks!
Yes:
sprintf("%d:%02d", floor($ms / 60000), floor($ms % 60000) / 1000);
I need to rotate an image at 12 midnight every day from a group of 5-10 images. How can I go about doing this with JavaScript or jQuery or even PHP?
At a basic level what you want to do is define an array of image names then take the number of days from a given point in time then modulo (remainder after division) by the number of images and access that index in the array and set the image, e.g. (untested code)
var images = new Array("image1.gif", "image2.jpg", "sky.jpg", "city.png");
var dateDiff = new Date() - new Date(2008,01,01);
var imageIndex = Math.Round(dateDiff/1000/60/60/24) % images.length;
document.GetElementById('imageId').setAttribute('src', images[imageIndex]);
Bear in mind that any client-side solution will be using the date and time of the client so if your definition of midnight means in your timezone then you'll need to do something similar on your server in PHP.
Maybe I don't understand the question.
If you just want to change the image write a batch file/cron job and have it run every day.
If you want to display a certain image on Monday, and a different one of Tuesday then do something like this:
<?php
switch(date('w'))
{
case '1':
//Monday
break;
case '2':
//Tuesday:
break;
...
}
?>
I'd do it on first access after midnight.
It doesn't even have to be in cron:
<?php
// starting date for rotation
$startDate = '2008-09-15';
// array of image filenames
$images = array('file1.jpg','file2.jpg',...);
$stamp = strtotime($startDate);
$days = (time() - $stamp) / (60*60*24);
$imageFilename = $images[$days % count($images)]
?>
<img src="<?php echo $imageFilename; ?>"/>
Edit: I totally misread this question as "without using javascript/PHP". So disregard this response. I'm not deleting it, just in case anyone was crazy enough to want to use this method.
Doing it without Javascript, PHP, or any other form of scripting language could be difficult. Well actually, it would just be very contrived, since it would be trivial with even the most basic JS/PHP.
Anyway, to actually answer your question, the only way I can think of doing it with vanilla HTML is to set up a shell script to run at midnight. That script would just rename your files. Do this with cron (on linux) or Windows Task Scheduler with a script kinda like this: (dodgy pseudo code follows, convert to whatever you're comfortable with).
let number_of_files = 5
rename current.jpg to number_of_files.jpg
for (x = 2 to number_of_files)
rename x.jpg to (x-1).jpg
rename 1.jpg to current.jpg
In your HTML, just do this:
<img src="path/to/current.jpg" />
And every day, current.jpg should change to something new. If you're using any sort of cache-control, make sure to change it so that it doesn't get cached for longer than a few hours.
If you are running a linux system you can set a Cron Job or you can use the windows task scheduler if you are on windows
You have two options.
In JavaScript, you basically have it choose an image based on the day of the week or day of the month if you have more than 7 images. Day of the month modulo by the length of the image array should let you pick the right array element.
You need something a bit more stateful to track what's going on... using SQL you can track when images are used and pick from the rotating list. You could also use a text file maintained by PHP to track the ordered list.
The old school way is to have a cron job rotate the image, but I'd avoid that these days.
That depends on how you are rotating them - sequentially, randomly, or what?
There are a number of options. You can determine which image you want in PHP, and dynamically change your <img> element to point to the correct location. This is best if you are already generating your pages dynamically.
You can always point to a single URL, which is a PHP file that determines which image to show and then performs a 302 redirect to it. This is better if you have static pages that you don't want to incur the overhead of dynamic generation for.
Don't make the image URL itself serve different images. You'll screw up your cache hit ratio for no good reason and incur unnecessary overhead on what should be a static resource.
Martin, "rotate" in this context means "change on a regular basis", not "turn around an axis".
It depends (TM) on what exactly you want to achieve. Just want to display a "random" image? Maybe this javascript snippet will get you started:
var date = new Date();
var day = date.getDate(); // thats the day of month, use getDays() for day of week
document.getElementById('someImage').src = '/images/foo/bar_' + (day % 10) + '.gif';
I assume by "rotate image" you mean "change the image in use" and not "rotational transformation about an axis" -- a simple way is to have a hash table that maps day modulo X to an image name.
$imgs = array("kitten.jpg", "puppy.gif","Bob_Dole.png");
$day_index = 365 * date("Y") + date("Z")
...
<img src="<? $imgs[$day_index % count($imgs)] ?>" />
(sorry if I got the syntax wrong, I don't really know PHP))
Set up a directory of images.
Select seven images and name them 0.jpg, 1.jpg, 2.jpg, 3.jpg, 4.jpg, 5.jpg, 6.jpg,
Using mootools Javascript framework with an image tag in HTML with id "rotatingimage":
var d=new Date();
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
var offset = -10; // set this to your locale's UTC offset
var desiredTime = utc + (3600000*offset);
new dd = new Date(desiredTime);
$('rotatingimage').setProperty('src', dd.getDay() + '.jpg');
Here's a solution which will choose a random image per day from a directory, which might be easier than some other solutions posted here, since all you have to do to get something into the rotation is upload it, rather than edit an array, or name the files in arbitrary ways.
function getImageOfTheDay() {
$myDir = "path/to/images/";
// get a unique value for the day/year.
// 15th Jan 2008 -> 10152008. 3 Feb -> 10342008, 31 Dec -> 13662008
$day = sprintf("1%03d%d", date('z'), date('Y'));
// you could of course get gifs/pngs as well.
// i'm just doing this for simplicity
$jpgs = glob($myDir . "*.jpg");
mt_srand($day);
return $jpgs[mt_rand(0, count($jpgs) - 1)];
}
The only thing is that there's a possibility of seeing the same image two days in a row, since this picks it randomly. If you wanted to get them sequentially, in alphabetical order, perhaps, then use this. (only the last two lines are changed)
function getImageOfTheDay() {
$myDir = "path/to/images/";
$day = sprintf("1%03d%d", date('z'), date('Y'));
$jpgs = glob($myDir . "*.jpg");
return $jpgs[$day % count($jpgs)];
}
How to rotate the images has been described in a number of ways. How to detect when to trigger the rotate depends on what you are doing (please clarify and people can provide a better answer).
Options include:
Trigger the rotate on 'first access after midnight'. Assumes some sort of initiating event (eg user access)
Use the OS scheduling capabilities to trigger the rotate (cron on *nix, at/task scheduler on Windows)
Write code to check time & rotate
Option 3 has the risk that a poorly coded solution could be overly resource intensive.