I want to show time slots between 2 times. Start time & End time. Used below code, it's working fine. It loops start and end time and add 30 mins to time. Starts from 2:00pm, 2:30pm, 3:00pm all the way till 10:00pm
$start_time = "14:00:00";
$end_time = "22:30:00";
#for ($i = strtotime($start_time); $i < strtotime($end_time); $i=$i+1800)
<li data-time="{{date('g:i A', $i)}}" class="timefield">{{date("g:i A", $i)}}</li>
#endfor
What I am stuck on is 2 parts
Hide past time, lets say right now is 4:00pm, it should hide past time slots i-e 2:00pm,2:30pm,:3:00pm,3:30pm
If right now is 4:00pm, it should start from 5:00pm all the way till 10:00pm. Adding extra buffer time of 1 hour.
You could insert the current timestamp in your logic like this:
$start_time = strtotime("14:00:00");
$end_time = strtotime("22:30:00");
$now = (new DateTime())->getTimestamp();
$nowRemaining = $now % 1800; // Divide to half hours & get the remaining seconds
$nowRounded = $now - $nowRemaining; // Round to half hours
$nextHour = $nowRounded + ($nowRemaining == 0 ? 3600 : 5400); // Add the extra buffer
for ($i = max($start_time, $nextHour); $i < $end_time; $i=$i+1800) {
...
}
Related
How do I get the amount of worked minutes from a crossed time range?
For example:
$begindate = "00:00";
$enddate = "08:00";
if(isSaturday($begindate)){
//00:00 - 06:00 || 149%
if(strtotime($begintime) >= strtotime("00:00") and strtotime($begintime) <= strtotime("06:00")){
$weekend149 = true;
}
}
The code currently sees that the person has worked between the hours but I want to get the specific amount he has worked BETWEEN that time range only.
So if he worked from 03:00 till 08:00 I want to have a separate variable that says 3 hours or 180 minutes.
You have two time intervals and are looking for the time span of the overlap. This can be done well with DateTime. Date and time should be used for correct calculations. If only a time is specified, the calculation is made for the current date.
$startdate1 = "03:00";
$enddate1 = "08:00";
$startdate2 = "00:00";
$enddate2 = "06:00";
$startdate1 = date_create($startdate1);
$enddate1 = date_create($enddate1);
$startdate2 = date_create($startdate2);
$enddate2 = date_create($enddate2);
$startOverlap = max($startdate1,$startdate2);
$endOverlap = min($enddate1,$enddate2);
$diff = $startOverlap < $endOverlap
? $startOverlap->diff($endOverlap)
: false
;
echo $diff ? $diff->format('%h:%i:%s') : 'no overlap';
The output is limited to 24 hours. If the difference can be more than 24 hours, you have to include the days in the format.
You can test different times yourself at https://3v4l.org/Fa1sE.
I am trying to add 15 minutes to given times till the end time is reached. I have been looking at loop over the time till it reaches end time with specific interval but this solution helps only for times within the same day.
In case as shown below it will not work.
$startTime = '16:00';
$endTime = '02:00';
The output I am looking for is as follow:
16:00
16:15
16:30
16:45
17:00
17:15
17...
.....
01:45
02:00
And let's say it is now 16:13 how can I then start the output from 16:30? or even better 30 mins later for example, at 16:45.
Any help is appreciated.
I think the next solution can help:
<?php
// get current time and round it to next half hour
$start = (intdiv(time(),(30 * 60)) + 1) * (30*60);
// since intdiv introduced in PHP 7 in previous PHP versoins
// intval() function can be used instead
$start = (intval(time()/(30 * 60)) + 1) * (30*60);
$end = strtotime('02:00');
// if end time less then start add one day
if ($end < $start) $end += 60 * 60 * 24;
while ($start <= $end) {
echo date('H:i',$start) . PHP_EOL;
// Increment Start date
$start += (15*60);
}
share PHP code
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);
}
I have a scenario in which the user selects a time and day (or multiple days) and that value must be converted to whatever that day and time would be in UTC time. I have the gmt offset amount for each user (the users set it when they signup). For instance:
A user in the eastern timezone selects:
3:15 pm, Monday, Tuesday, Friday
I need to know what time and days that information would be in UTC time. The solution has to take into situations such Monday in one timezone can be a different day in UTC time. Also, if the time can be converted to 24 hour format, that would be a plus.
For the sake of clarity, something along the lines of an array should be returned such as:
Array('<3:15 pm eastern adjusted for utc>', '<Monday adjusted for UTC>', '<Tuesday adjusted for UTC>', '<Friday adjusted for UTC>');
I don't need the result to be directly formatted into an array like that - that's just the end goal.
I am guessing it involves using strtotime, but I just can't quite my finger out how to go about it.
$timestamp = strtotime($input_time) + 3600*$time_adjustment;
The result will be a timestamp, here's an example:
$input_time = "3:15PM 14th March";
$time_adjustment = +3;
$timestamp = strtotime($input_time) + 3600*$time_adjustment;
echo date("H:i:s l jS F", $timestamp);
// 16:15:00 Monday 14th March
EDIT: kept forgetting little things, that should be working perfectly now.
Made a function to do the job:
<?
/*
* The function week_times() converts a a time and a set of days into an array of week times. Week times are how many seconds into the week
* the given time is. The $offset arguement is the users offset from GMT time, which will serve as the approximation to their
* offset from UTC time
*/
// If server time is not already set for UTC, uncomment the following line
//date_default_timezone_set('UTC');
function week_times($hours, $minutes, $days, $offset)
{
$timeUTC = time(); // Retrieve server time
$hours += $offset; // Add offset to user time to make it UTC time
if($hours > 24) // Time is more than than 24 hours. Increment all days by 1
{
$dayOffset = 1;
$hours -= 24; // Find out what the equivelant time would be for the next day
}
else if($hours < 0) // Time is less than 0 hours. Decrement all days by 1
{
$dayOffset = -1;
$hours += 24; // Find out what the equivelant time would be for the prior day
}
$return = Array(); // Times to return
foreach($days as $k => $v) // Iterate through each day and find out the week time
{
$days[$k] += $dayOffset;
// Ensure that day has a value from 0 - 6 (0 = Sunday, 1 = Monday, .... 6 = Saturday)
if($days[$k] > 6) { $days[$k] = 0; } else if($days[$k] < 0) { $days[$k] = 6; }
$days[$k] *= 1440; // Find out how many minutes into the week this day is
$days[$k] += ($hours*60) + $minutes; // Find out how many minutes into the day this time is
}
return $days;
}
?>
Suppose the target time is 4.30 pm and the current time is 3.25 pm , how will i calculate the minutes remaining to reach the target time ? I need the result in minutes.
session_start();
$m=30;
//unset($_SESSION['starttime']);
if(!$_SESSION['starttime']){
$_SESSION['starttime']=date('Y-m-d h:i:s');
}
$stime=strtotime($_SESSION['starttime']);
$ttime=strtotime((date('Y-m-d h:i:s',strtotime("+$m minutes"))));-->Here I want to calcuate the target time; the time is session + 30 minutes. How will i do that
echo round(abs($ttime-$stime)/60);
Krishnik
A quick calculation of the difference between two times can be done like this:
$start = strtotime("4:30");
$stop = strtotime("6:30");
$diff = ($stop - $start); //Diff in seconds
echo $diff/3600; //Return 2 hours. Divide by something else to get in mins etc.
Edit*
Might as well add the answer to your problem too:
$start = strtotime("3:25");
$stop = strtotime("4:30");
$diff = ($stop - $start);
echo $diff/60; //Echoes 65 min
Oh and one more edit:) If the times are diffent dates, like start is 23:45 one day and end is 0:30 the next you need to add a date too to the strtotime.