PHP for loop dates - php

I'm trying to create a for loop where I specify a beginning date and end date and I want to pull a random mix of dates where each date will always be later than it predecessor and earlier than the next number.
There is no pre-defined set of dates I need, I may have 10 - I may have 100 or 1000 as long as they exist within the start and end dates.
Example:
2015-01-15 08:06:00
2015-01-15 15:23:42
2015-01-16 06:03:00
.........
.........
2015-01-20 08:18:32
This is what I have (which is wrong):
$start = strtotime('2015-01-10 08:00:00');
$end = strtotime('2015-01-20 10:57:59');
for ($i=$start;$i<$end;$i++) {
//Current time + Anywhere between 1 hour and 3 days
$x = $i + (int)rand(3600,259200);
echo "<p>" . date('Y-m-d H:i:s', $x) . "</p>";
}
In my tests, I'm not getting any results...I believe the loop is running infinitely...or something else is wrong.
Can anyone help?

If I gather your question correctly, it seems like you're going about it the wrong way. The easiest way would be to generate random times in a range (between $start and $end) and then sort them. That way you don't have to guess at how much time-space you have left when choosing a random interval.
$start = strtotime('2015-01-10 08:00:00');
$end = strtotime('2015-01-20 10:57:59');
$num_randoms = 10;
for ($i=0;$i<$num_randoms;$i++) {
$dates []= (int)rand($start,$end);
}
sort($dates);
foreach ($dates as $x) {
echo "<p>" . date('Y-m-d H:i:s', $x) . "</p>";
}

You assumed that those two will be your dates
$start = strtotime('2015-01-10 08:00:00');
$end = strtotime('2015-01-20 10:57:59');
You assumed also that you will use specific range of seconds for pseudorandom number generation
$min = 3600; // 1 hour
$max = 259200; // 72 hours (3 days)
The first step you need to do is to reduce the ending number of seconds with the range maximum. This will assure you that latter date will be before the specified date
$end -= $max;
And at the end here is the proper loop. I used while loop because it is more readable.
while ($start < $end) {
$start += (int) mt_rand($min, $max); // add the generated value to the start seconds
echo date('Y-m-d H:i:s', $start);
}
PS. Notice I used the mt_rand function

Use the milliseconds representation of you date value. Pseudo-code is as follows:
Convert the start date to its milliseconds representation sm
Convert the end date to its milliseconds representation em
For i = 0 up to n, n is the number of dates...
3.1 randomize some value between sm and sm + offset
3.2 convert the generated value to its date object representation
3.3 assign sm = sm + offset and check if sm < em
offset is the size of the sampling interval, offset <= (em - sm) / n

Related

Calculating A Cycle number using dates for a 28 day repeating cycle

I have a job that runs every 28 days. and I want to assign it a cycle number based on a starting reference date.
e.g
1st cycle is 01/27/22. and that cycle number would be 2201.
subsequently I want to calculate the cycle number based on the current date. but for each year there could be either 13 or 14 cycles.
I've managed to figure out the number of cycles since the reference date to figure out the latest cycle date (see below)
const REF_ZERO_DATE = '01/27/2022';
const REF_ZERO_CYCLE_YEAR = "22";
const REF_ZERO_CYCLE_NUM = "01";
$today = new \DateTime("2023/12/29");
echo ("Today = ".$today->format("Y/m/d")."\n");
$ref_zero = new \DateTime(self::REF_ZERO_DATE);
echo ("ref_zero = ".$ref_zero->format("Y/m/d")."\n");
$number_of_days_since_ref_zero = $today->diff($ref_zero)->format("%a");
echo ("Number of days since ref zero = ".$number_of_days_since_ref_zero."\n");
$number_of_cycles_since_ref_zero = floor($number_of_days_since_ref_zero/28);
echo ("Number of cycles since ref zero = ".$number_of_cycles_since_ref_zero."\n");
$interval = 'P' . $number_of_cycles_since_ref_zero*28 . 'D';
echo ("Interval = ".$interval);
$date_of_lastest_cycle = date_add($ref_zero,new \DateInterval($interval));
echo ("last Cycle Date = ".$date_of_lastest_cycle->format("Y/m/d")."\n");
But my math for the cycle adjustment is missing coping with 12 or 13 cycle in a specific year.
It is not explicitly stated whether the cycle of the previous year continues into the next or not.
The scenario in which the cycles can overlap between years is more complicated, so this is assumed.
The interval count code was extracted to the following function:
function calculateIntervalCount($startDate, $endDate, $interval) {
$start = new \DateTime($startDate);
$end = new \DateTime($endDate);
$interval = new \DateInterval($interval);
$periodDays = intval($end->diff($start)->format('%a'));
$intervalDays = intval($interval->format('%d'));
return floor($periodDays / $intervalDays);
}
There are two cases when calculating the interval count of a particular year:
year of start and end are the same year
year of end is after year of start
In the first case the interval count is the same as the interval count of the whole period.
In the second case the interval count of a particular year can be calculated from the difference between the interval counts of the whole period and the period before the end year.
The following function returns the cycle number:
function calculateCycleNumber($startDate, $endDate, $interval) {
$totalCycles = calculateIntervalCount($startDate,$endDate,$interval);
$startYear = intval((new \DateTime($startDate))->format('Y'));
$endYear = intval((new \DateTime($endDate))->format('Y'));
if($startYear < $endYear) {
$endOfLastYearDate = (new \DateTime($endDate))->modify('last day of December last year')->format('Y-m-d');
$cyclesSinceEndOfLastYear = calculateIntervalCount($endOfLastYearDate, $endDate, $interval);
$yearCycle = $totalCycles - $cyclesSinceEndOfLastYear + 1;
} else {
$yearCycle = $totalCycles;
}
$yearCode = substr($endYear,-2);
$yearCycleCode = sprintf('%02d', $yearCycle);
return $yearCode . $yearCycleCode;
}
A cycle number of 2314 was obtained with the inputs provided.
echo calculateCycleNumber('01/27/2022','2023/12/29','P28D');
Note that 14 is possible in case of overlapping cycles.
You can use timestamp, where you add 28 days each time so you get the next date and so on.
Get the next timestamp
$next_date = strtotime('+28 day', $timestamp);
Convert to readable date
echo date('m/d/Y', $next_date);

PHP Get last increment of five minutes [duplicate]

This question already has answers here:
Subtract time in PHP
(3 answers)
Closed 3 years ago.
I am looking to get the previous increment of five minutes from the current time...
Lets say that the current time is 12:07pm UTC
I want to put into a variable 12:05pm UTC
What would be an easy way of going about this?
You can do this with the DateTime class (https://3v4l.org/7aqMH):
<?php
$date = new DateTime('12:07 UTC');
$minutes = (int) $date->format('i');
$rounded = round($minutes / 5) * 5;
$date->setTime($date->format('H'), $rounded, $date->format('s'));
or more concisely:
<?php
$date = new DateTime('12:07 UTC');
$date->setTime(
$date->format('H'),
round($date->format('i') / 5) * 5,
$date->format('s')
);
There's nothing built in that allows you to retrieve 'increments' however you can calculate it with minimal code. The use of modulo here allows you to figure out how far past the most recent 5 minute mark is. It will never be greater than 5 minutes (300 seconds) and can always be subtracted safely to take you back to the time you want.
$now = time();
echo $now . PHP_EOL;
$five_minutes = 60*5; // 300
$offset = $now % $five_minutes;
$five_block = $now - $offset;
echo date('Y-m-d H:i:s', $five_block);
First you will want to extract the minutes from their stored variable. Then mathematically this is simple by applying division and the floor function. To do this you can first divide by five using intdiv(numerator, denominator) which will remove any trailing decimal points and then multiply by five again to get your desired value at an increment of five.
Get the current time using time() :
$min = 300
$currentTime = time();
$mod = $currentTime % $min;
$res = $currentTime - $mod;
finalResult = date('Y-m-d H:i:s', $res);

Splitting a time range into hours?

I'm currently working with a data set containing a start time, date, and a duration in seconds, and need to be able to split the range of times from start time to (start time + duration, e.g. end time) into hour "buckets" if you will.
So for instance, a start time of 08:30:00 with duration 9000 seconds (2.5 hours) covers hour 08 from 8:30:00 to 09:00:00 for 1800 seconds, hour 09 until 10:00:00 for 3600 seconds, etc. up to the end time of 11:00:00.
What would be the best way possible to do this? I'm working in PHP however a general algorithmic solution would still be incredibly helpful.
My current approach is as follows:
1. Calculate the hour difference between both times (round up to next hour if there's a non-zero number of minutes)
2. Let session progress = 0
3. Iterate the range from 1 to hour difference:
1. Let the current bucket duration = min(3600, duration)
2. Let the duration = max(0, duration - current bucket duration)
3. Let bucket start time = start time + session progress
4. Let bucket end time = bucket start time + current bucket duration
5. Do work based on these values
6. Let session progress += current bucket duration
My PHP implementation of this is as follows:
foreach ($csv as $listen_record) {
$start_dt = $listen_record['Date'] . ' ' . $listen_record['Time'];
$session_duration = $listen_record['Duration'];
$session_start_time = date_create_from_format($datetime_format, $start_dt);
$session_end_time = clone $session_start_time;
date_add($session_end_time, date_interval_create_from_date_string($session_duration . ' seconds'));
$diff = date_diff($session_end_time, $session_start_time);
$hourdiff = $diff->h;
if ($diff->m > 0)
$hourdiff += 1;
$session_progress = 0;
foreach (range(1, $hourdiff) as $hour) {
$record_duration = min(3600, $session_duration);
$session_duration = max(0, $session_duration - $record_duration);
$record_start_time = clone $session_start_time;
date_add($record_start_time, date_interval_create_from_date_string($session_progress . ' seconds'));
$record_end_time = clone $record_start_time;
date_add($record_end_time, date_interval_create_from_date_string($record_duration . ' seconds'));
if ($record_start_time == $record_end_time)
continue;
// DO WORK...
$session_progress += $record_duration;
}
}
This kind of works to separate each record into hour long buckets however gives some weird results for some cases (particularly cases where the range crosses into a new day) and doesn't align to actual wall clock hours.
Is there a better way to do this that can actually align to wall clock hours and not freak out when it has to cope with durations that cross over midnight?
Thanks!
Update: I managed to solve this on my own so I'm posting how I did it just in case anyone else needs to do similar.
Basically I did it by creating a range between the start and end timestamp, and filtering out every time stamp apart from start and end that isnt a multiple of 3600 or just before 3600. This gives me the wall clock hours in the middle of the range as timestamps. I can then split the array into chunks of 2 which contains the slot start time and end time.
My PHP code is as follows and now actually works:
function is_wall_clock_aligned_hour_boundary($timestamp) {
return ($timestamp % (60*60) === 0) || (($timestamp + 1) % (60*60) === 0);
}
function create_slot_object($slot) {
$start_time = reset($slot);
$end_time = end($slot);
$duration = $end_time - $start_time;
return array(
'startTime' => convert_timestamp_to_datetime($start_time),
'endTime' => convert_timestamp_to_datetime($end_time),
'duration' => $duration
);
}
function make_slots(DateTime $start_time, $duration) {
$start_timestamp = $start_time->getTimestamp();
$end_time = clone $start_time;
date_add($end_time, date_interval_create_from_date_string("$duration seconds"));
$end_timestamp = $end_time->getTimestamp();
$time_sequence = range($start_timestamp, $end_timestamp);
$slot_boundaries = array_filter($time_sequence, 'is_wall_clock_aligned_hour_boundary');
array_unshift($slot_boundaries, $start_timestamp);
array_push($slot_boundaries, $end_timestamp);
$slots = array_chunk($slot_boundaries, 2);
return array_map('create_slot_object', $slots);
}

PHP: Wrong Time Calculation

I am working on a project and writing a function to add two different times. The times are stored in database as a string.
I'm:
Pulling value from db
converting it into time using strtotime
adding times using date function
Here is my code:
$time_1 = '1:00';
$time_2 = '0:05';
//should be 1:05, whereas it prints 04:05
echo date("H:i", strtotime($time_1) + strtotime($time_2));
Please tell me, what is wrong with above code and how it can be fixed?
Thanks
Your problem is because strtotime returns the number of seconds since the Unix Epoch (Jan 1 1970). So what you are getting is not values of 60 and 5, but something more like 1537570800 and 1537567500. When you add those two values together, you end up with a date far in the future, with what looks effectively like a random time. To compensate for this, you need to subtract the value of strtotime at the start of the day to make the second time a relative time e.g.:
echo date("H:i", strtotime($time_1) + strtotime($time_2) - strtotime('00:00'));
Output:
01:05
Update
Since it turns out that the sum of the two times can exceed 24 hours, the above code will not work (the maximum time it will display is 23:59 before rolling over to 00:00. So it is necessary to convert both times to a relative number of minutes to the start of the day, add them and then display as hours and minutes:
$time_1 = '12:00';
$time_2 = '14:30';
$time_sum = (strtotime($time_1) + strtotime($time_2) - 2 * strtotime('00:00')) / 60;
printf('%02d:%02d', intdiv($time_sum, 60), $time_sum % 60);
Output:
26:30
Use DateTime::createFromFormat function, and taking ideas from Adding two DateTime objects in php
$time_1 = '1:00';
$time_2 = '0:05';
$t1 = DateTime::createFromFormat('G:i', $time_1);
$t2 = DateTime::createFromFormat('G:i', $time_2);
$interval1 = $t1->diff(new DateTime('00:00:00')) ;
$interval2 = $t2->diff(new DateTime('00:00:00')) ;
$e = new DateTime('00:00');
$f = clone $e;
$e->add($interval1);
$e->add($interval2);
$total = $f->diff($e)->format("%H:%I:%S");
Additional Details:
G and H 24-hour format of an hour with or without leading zeros
i Minutes with leading zeros 00 to 59

PHP Get Time From X Percentage of Total Time

I don't know if this will make sense but I would like to get the time from percentage of a value, I have the following as an input example:
mypercentage = 50;
mytime = "00:59:59"
As you can see, my time is 60 minutes (1 hour) and 50% of that is 30 minutes therefore from the 2 above inputs I would like to get the following output
(50 % of an hour):
00:30:00
Explode the string into an array
Convert the 3 elements into numbers
Find the value in seconds. $array[0]*3600+$array[1]*60+$array[2]
Calculate the time from the value in step 3 and the percentage.
Use /3600 and /60 to find the hour and minutes and %60 to find the seconds.
put the values in an array and implode to a string, or just use
$hour.":".$minute.":".$second
Thank you all who responded, I found this solution to work:
$mypercentage = 50;
$mytime = '00:59:59';
$totaltime = date('H:i:s', strtotime($mytime));
$seconds = strtotime("1970-01-01 $totaltime UTC");
$per_seconds = ($mypercentage / 100) * $seconds;
$time_from_percentage = gmdate("H:i:s", $per_seconds);
echo $time_from_percentage;

Categories