I want to divide time to an integer value:
1-> 6.15AM till 7.00AM
2-> 7.00AM till 7.45AM
3-> 7.45AM till 8.30AM
4-> 8.30AM till 9.15AM
------BREAK----------- 15 minutes
5-> 9.30AM till 10.15AM
6-> 10.15AM till 11.00AM
7-> 11.00 till 11.45AM
-------BREAK---------- 15 minutes
8-> 12.00 till 12.45PM
9-> 12.45PM till 1.30PM
10-> 1.30PM till 02.15PM
Anyone can help me...?
thanks before..!
check out http://php.net/manual/en/function.mktime.php it might me able to help you, try converting the times into unix timestamps maybe?
Try, you might have to change the ranges a bit (i am too lazy to do the exact calculation)
$time = time(); // or if your time is in string format use $time = strtotime('10:15 AM');
$seconds_elapsed = $time % 86400; // Seconds since midnight
$mins_elapsed = $seconds_elapsed / 60;
$mins_split = floor($mins_elapsed / 15); // Split into 15 mins intervals
if ( $mins_split >= 25 && $mins_split <= 36) {
$index = ($mins_split - 25) / 3;
} elseif ($mins_split >= 38 << $mins_split <= 47) {
$index = ($mins_split - 38) / 3;
} else {
$index = ($mins_split - 48) / 3;
}
Related
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) {
...
}
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
Ok so I am trying to schedule a campaign with mailchimp API and so far everything works fine except for the last else if that doesn't put 00 in my variable. I know php will put only 1 zero in the array and can't think of a work around for this. Help ! The code below is used to take a date, put it in iso 8601 format and then I use substr_replace() to schedule the campaign to the next time lapse available for mailchimp (15, 30, 45, 00).
I tried to change it for a string "00" instead but mailchimp does not schedule it.
$date_temp = new DateTime($date_debut);
$date_debut_iso = $date_temp->format('c');
$test = explode(':', $date_debut_iso);
//campaign has to be scheduled on :00, :15, :30, :45
if($test[1] >= 0 && $test[1] <= 15){
$test[1] = 15;
}else if($test[1] >= 16 && $test[1] <= 30){
$test[1] = 30;
}else if($test[1] >= 31 && $test[1] <= 45){
$test[1] = 45;
}else if($test[1] >= 46 && $test[1] <= 59){
$test[1] = 00;
}
$new_date = substr_replace($date_debut_iso, $test[1], 14, 2);
i need the last else if to store 00 in my array $test[1] instead of 0. String doesn't work.
My only guess as to why setting $test[1] = '00'; wouldn't have worked is that all the other cases increase the time (or leave it the same), while that case decreases it because you're setting the minute to zero without changing the hour.
You don't really need to do all the string manipulation though. You can just get the minutes from the DateTime object and do a little math, then output the final result with ->format().
$date_temp = new DateTime($date_debut);
if ($offset = $date_temp->format('i') % 15) {
$date_temp->modify('+' . (15 - $offset) . ' minutes');
};
$new_date = $date_temp->format('c');
The calculation is basically: if the remainder of the minutes divided by 15 is non-zero, increase the time by 15 - remainder minutes. This will also increment the hour appropriately for the > 45 case.
I'm getting the opening and closing time of a store in HHMM format (0000 to 2359):
$openingtime = intval($open->time);
$closingtime = intval($close->time);
Then I get the current time in the same format:
$nowtime = intval(date("G") . date("i"));
I want to know whether the current time is between 15 minutes before the opening time, and 45 minutes before the closing time.
I have this, but it is inaccurate:
if(($nowtime >= ($openingtime - 15)) && ($nowtime <= ($closingtime - 45))){
// current time is between 15 minutes before opening and 45 minutes before closing
}
If the time is 2300, then 2300 - 15 = 2285, which isn't a valid time.
How can I resolve this?
Also, I assume I need to do something at the overlap of the day (0000), but I'm not sure what I need to do there.
You can create DateTime object like that:
$open = date_create_from_format('Hi', '1000');
$close = date_create_from_format('Hi', '1900');
Then you can use DateTimeInterval
$interval15 = new \DateInterval('P0Y0DT0H15M');
$interval45 = new \DateInterval('P0Y0DT0H45M');
Then you can sub it from closing time and from open:
$now = new DateTime();
if ($now >= $open->sub($interval15) && $now <= $close->sub($interval45)) {
// logic
}
You should convert from HHMM into minutes and then compare minutes with minutes
$time_open = str_pad($openingtime, 4, '0', STR_PAD_LEFT);
$time_close = str_pad($closingtime, 4, '0', STR_PAD_LEFT);
$open_minutes = substr($time_open,0,2) * 60 + substr($time_open,2,2);
$close_minutes = substr($time_close,0,2) * 60 + substr($time_close,2,2);
$nowtime = date("G") * 60 + date("i");
if($nowtime >= $open_minutes - 15 AND $nowtime <= $close_minutes - 45)
{
// do your stuff
}
I am having problem detecting if the shop is still open or closed after midnight.
On Tuesday ($weekday = 2), shop open from 6 PM to 1:30 AM (after midnight)
Assume current time is 01:05 AM
$weekday = 2;
//Convert current time to minutes (01:05)
$currentTime = ($weekday - 1) * 1440 + "01" * 60 + "05";
//Current Week Day
$shopOpenTime = "18:00";
$shopCloseTime = "01:30";
$open = explode(':', $shopOpenTime);
$close = explode(':', $shopCloseTime);
//Convert to Minutes;
$MinutesOpen = (($weekday - 1) * 1440) + ($open[0] * 60 + $open[1]);
$MinutesClose = (($weekday - 1) * 1440) + ($close[0] * 60 + $close[1]);
if ($MinutesClose < $MinutesOpen)
$MinutesClose += 60 * 24;
if (($currentTime >= $MinutesOpen) && ($currentTime < $MinutesClose)) {
echo "Shop Is Open";
} else {
echo "Shop Is Close";
}
What is the solution to fix this issue?
Honestly this entire chunk of code should be re-written. The logic is doomed because your $weekday parameter is comparing the same day for $currentTime as $minutesOpen. You added 24 hours to $minutesClose to push it to (technically Wed), now that comparison is correct. It says Closed because you haven't moved $currentTime up 24 hours into wed.
So if you were to just read this in english you're comparing 1:30am on Tuesday (which is right after monday night) against 6:30 pm on Tuesday, and this fails (this is because of the $weekday parameter is set to tuesday in the $currentTime).
Without seeing how you populate your $weekday parameter, I'm guessing that when you actually run this code being poplated with say the date() function, at 12:01 (on tuesday) it's going to increment to wed, then you're code will be populating open and close time with wed's hours, which is going to be off too.
I'm sorry I know this isn't a solution saying to re-write the entire logic, but it's going to be massively flawed in a production environment.
If you just want to make it work, staying with the mindset/context of 'tuesday' you have to reflect current time in tuesday's sense, and thats with 25:05 meaning 1:05 on wed, and not 01:05 tuesday for the second time. That keeps the context correct and it works.
$currentTime = ($weekday - 1) * 1440 + "25" * 60 + "05";
You are comparing the current time to the opening period that will begin on the current time's day. But the shop is currently open due to an opening period that started yesterday. Just compare with yesterday's period also:
if ((($currentTime >= $MinutesOpen - 1440) && ($currentTime < $MinutesClose - 1440)) || (($currentTime >= $MinutesOpen) && ($currentTime < $MinutesClose))) {
But beware, this works only, if the shop opens / closes every day at the same time.