I'm trying to make a simple function, that echos a random element from an array, but it should change, every time that day changes. Example:
$myarray = array(
'foo',
'bar',
'winnie',
'the',
'poo');
echo array_rand($myarray);
This would print a random thing from the array, every time the page loads. But I would like for it to only change each day. I would like for it to work, so if you load the page Monday at 8:00 and Monday at 17:00 (edit1: it's the same random output that has been pulled, regardless of, if it's user A or user B that sees it), then you would see the same (random) element, from the array. But if the page was loaded at Tuesday at 13:00, then a different element from the array would be printed. Edit1: The timezone should be the easiest to program (since it's not significant). So I assume that the servers timezone would be the easiest.
I thought about getting integer-value of the date, and then use modulo to the length of the array (since the length of the array will get more and more values with time). Something along the lines of echo $myarray[date(U) % count($myarray)] (this hasn't been tested and wont work, since it's the second since 1970 (or whenever) and not the days, but it was just to give an idea of what kind of solution I had in mind).
There is no database on the site, so I can't store the value in a database.
Edit2: So if we have user A and user B, that each load the page each day of the week. Then I'm looking for user A to receive something along these lines:
Monday: foo
Tuesday: the
Wednesday: foo
Thursday: poo
Friday: winnie
Saturday: winnie
Sunday: bar
And if user B load the page, then he will see the same values as user A (I assume this is the easiest way to set it up as well).
-- end of edit2 --
Edit3: It could also just be a txt-file or a json-file that is stored on the FTP-server, where each day, a new line of this txt-file is printed. It doesn't need to be an array.
-- end of edit3 --
You can do this with a simple text file on the server:
$randoms = array(
'foo',
'bar',
'winnie',
'the',
'poo'
);
$file = 'todaysrandom.dat';
if(file_exists($file) && date('d-m-Y', filemtime($file)) == date('d-m-Y')){
//if the file exists, and was last updated today, return the contents of the file:
$todaysRandom = file_get_contents($file);
}else{
//else create todays value, and save it to the file
$todaysRandom = $randoms[rand(0,count($randoms)-1)];
file_put_contents($file, $todaysRandom);
}
echo $todaysRandom;
Related
I'm trying to populate an array with every particular weekday, say Sunday, between a start date and an end date.
The best I could think of is as below. (The dates are actually Carbon dates.)
This seems to work, except that I have noticed when I dd() the result, all array elements are assigned the final value of $d (= $endDate_Planned). If I info() log these I see that its working at the point in time each is pushed into the array, but obviously the values are some sort of reference that gets updated every time $d updates/increments.
Is there a way to avoid that?
for( $d = $startDate_Planned; $d <= $endDate_Planned; $d->addDays(7) ) {
info($d);
$dateArray_Planned[] = $d;
}
That's ugly. The reason is that $d is an object. Every element of $dateArray_Planned is a reference to the same object.
I wouldn't use a for, but if I had, I'd just try copy()ing the object on each iteration.
$dateArray_Planned[]=$d->copy();
I'm simply trying to check if the time is either above 9am or below 6pm, the 6pm check works fine and displays the data where as 9am one does nothing but works when changed to 10am.
Here is the line of code I'm using
elseif ((($data[6]) < "09:00") || (($data[6]) > "18:00"))
{
$contact[] = $data;
}
So, let us assume that your time format is always hh:mm. You want to check if the time is between 9 and 18 hours. The problem in your code, as you described, comes when you are check again 09:mm. As it strips away the minute part and just compares the hours.
I would try something like this:
elseif ( implode("", explode(":", $data[6])) > 900 && implode("", explode(":", $data[6])) < 1800)
I will explain the approach step by step.
First of all, this code checks if the date is within the range of 09:00 to 18:00. The code you have provided tested if it is less than 9 or more than 18.
Let us focus on one part of the code, that will explain the whole thing as well:
implode("", explode(":", $data[6])) > 900
First, we separate hours and minutes using the explode function. This gives us an array with two values.
`[0] => hh,`
`[1] => mm`
Now that we have separated this value we implode or concatenate them using the implode function that has no separator.
Next, instead of testing against the string, we test against the number. 09:00 is the same as 900 in this case.
Thus, we can check if the time is within the required limits.
First of all, I know I am not supposed to as "code sample" here. Sadly, I am not a programmer and I have a situation where I need to update a line in a report to present to the customer, but I do not know how to do it.
I have access to PHP file report.php. In the same server and folder as report.php there is a file called report.csv. When report.php is loaded in browser, I want to show one line which will say:
Report.cvs is X minutes old that is all.
If the report is 10 days old, then also I can show the age in minutes. I dont need any complicated X days, Y hours, Z mins etc.
I am worried i might break something in server if I try to add myself since I am not programmer. Is there anyone who can show me what I need to add to report.php to make this work?
Looking through the documentation http://php.net/manual/en/function.fstat.php you might find that filectime ( string $filename ) may be useful.
Now if the file is consistently updated by users you may find that storing the creation/upload time in a Database like SQL/sqlite may be useful.
Ok so basically you need to get the time when the file was last modified, subtract it from the current time, convert the result to minutes and voilĂ .
Here's what that should look like:
$file = ''; // --- Path to the file
$filetime = filemtime($file); // --- File time
$now = time(); // --- Current Time
$fileAge = round(($now - $filetime)/60); // --- Subtract file time from current time, divide by 60 for the minutes and round the result.
echo basename($file).' is '.$fileAge.' minutes old.';
I'm out of ideas here.
In my DB table, I have all sorts of entries and they all have a row with start_time and duration. In this situation, the only way I could've entered the values was:
10:00 is entered in the db as 1000, and 23:00 is entered as 2300.
The durations are 130 for 1 and a half hours or 20 for twenty minutes and so on.
They are entered just as you would expect them to be without any symbols separating the digits.
The problem is, I just can't add the numbers and need to get the end_time calculated by adding the two values.
As an example, If i have an event starting at 1045 and it lasts 45 , I just need to echo 1130.
I'll gladly change it to 0900
You can try following:
$timed = strtotime("+".$duration." minutes", strtotime($time));
echo date('Hi', $timed);
First you need to convert the time and add your duration. After that, you can echo it as desired.
For eg.
$timed = strtotime("+130 minutes", strtotime("0900"));
echo date('Hi', $timed);
It will output
1110
What I'm attempting to do is create a simple "Quote of the Day" script. What this needs to do is be able to read from a simple .txt file, grab each entry per line and output the contents of that line, once per day. For example, if a .txt file had the following text:
This is the quote of the day
This is another quote of the day
This is the last quote of the day
Then, the script would grab the first block of text, This is the quote of the day and output it on the site. It would then cycle through, line by line, based on each incremental day until the end (and then cycle back to the beginning). Hopefully this would just allow people to cut/paste new info in as it would rely on line numbers, not the content itself.
If anyone even knows of a .XML implementation of this - it would be a big help - trying to figure out the simplest way of going about this. Thanks!
Assuming you had a file with 365 lines (one line per the current day)...
$lines = file("quotes.txt");
$day = date("z");
echo $lines[$day];
You can save the current index line and day on the first line of the file, like this:
01;09-11-2011
This is the quote of the day
This is another quote of the day
This is the last quote of the day
To retrieve the quote you would check if the date is today, if it is you get the nth line otherwise you add 1 to the number, update the date and then get the quote.
This should work with any number of lines in your textfile (untested):
// get lines
$lines = file('lines.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
// snap to midnight
$day = mktime(0,0,0, date("n"), date("j"), date("Y")) / (3600*24);
// modulo fun
echo $lines[ $day % count($lines) ];
It's very simple.
1.) You create a text file with 7 lines, each line represents a quote.
2.) PHP: You have to load the file into an array and get the numeric representation of the current day of the week:
$quotes = file('your_file.txt');
$the_quote = $quotes[ date('w') ];
echo $the_quote;
From php.net:
w
Numeric representation of the day of the week
0 (for Sunday) through 6 (for Saturday)
If you want to have a quote for each day in a year, just create a file with 366 lines and use date('z').
The most flexible way is definitely:
$day = date("z");
$file = file('quotes.txt');
$file_length = count($file);
$quote = $file[$day % file_length];
By using the modulus of the day and file length you have a recurring cycle every day from the first line to the last line of the file (and then start again).