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.
Related
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
How can I calculate the nearest hours to midnight time 00:00 regardless of date in PHP. For example:
If time is 22:00 then 2 hours are required to reach 00:00
If time is 04:00 then -4 hours are the nearest to reach 00:00
Currently I have the following PHP function:
<?php
$ts1 = strtotime('00:00');
$ts2 = strtotime('04:00');
$diff = ($ts1 - $ts2) / 3600;
?>
But this won't be helpful much in the above.
If you have the php Datetime class available you can calculate the difference between two DateTimes.
$time1 = new \DateTime('00:00');
$time2 = new \DateTime('04:00');
$diff = $time1->diff($time2, true);
$hourDifference = 0;
if ($diff->h < 12) {
$hourDifference = -$diff->h;
} elseif ($diff->h > 12) {
$hourDifference = 24 - $diff->h;
} else {
$hourDifference = 12; // kann be positive or negative
}
And you'll get a DateInverall object where you can access, hours, minuts, seconds and compare them with normal php operators.
If you'r not too interested in minutes;
1. Extract minutes.
check if minutes is > or <=30
if greater, 'store' 1
2. Extract hour
check if hour is greater than 12
if not, add 12 (store flag also to say it will be minus)
3. if greater (ref. Step 1), add 1 to extracted hour.
4. 24 - extracted hour is your interval.
Please note, this may be reduced/ simplified greatly.
Your interval (should) be correct to the nearest half hour
The answer depends on the date (not only the time). This is because of daylight saving time changes. For example might 02:59 being closer to 00:00 then 21:01 on the time where daylight saving time will set back hour.
I am trying to make a live chat link appear on the website only during business hours. I have the code below which seems to work in the afternoon, but won't work in the morning and I'm not sure why... $start and $end are values received from a MySQL database but in my example I've hard coded them to make the example simpler.
$LinkStatus = "on";
$start = 9:00:00;
$end = 23:00:00;
$current_time = date('G:i:s'); //9:35:00
if (($start > $current_time) || ($end < $current_time)) {
$LinkStatus = "off";
}
If the start time is greater than the current time, then the business is not open yet. If the end time is less than the current time, then it's after hours. Any time between 9am and 11pm (23:00) neither one of those conditions should be true, therefore $LinkStatus should remain "on". However, it does not seem to be doing that right now. Something is setting it to "off".
I've echoed the variable above the if statement and below it so I can confirm it's this if statement causing the variable to be set to "off".
As you can probably see from my code example, I'm not very knowledgeable when it comes to PHP. Any help is appreciated.
date('G:i:s') // 24 hours time without leading zero for hour
...won't sort well as a string, for example '9' > '10'.
Use 24 hour time with a leading zero instead, which makes the correct sort '09' < '10';
date('H:i:s') // 24 hour time with leading zero for hour
date_default_timezone_set('Europe/London');
$day_start = '09:00:00';
$day_end = '16:59:59';
$current_time = date('H:i:s'); // For 9-5 hours only
$current_day = date('N'); // For Monday to Friday only
if (($current_day <= 5) && ($current_time >= $day_start) && ($current_time <= $day_end)) {
echo 'ON';
} else {
echo 'OFF'
}
I have an simple question about how to set date based on time range. This is my code so far:
date_default_timezone_set("Asia/Jakarta");
$time = date("G:i");
if ($time >= 8:00)
{
echo date("j-F-Y");
}
else
{
echo date("j-F-Y", time() - 60 * 60 * 24);
}
Example today is 29-Apr-2013.
Now I want before time 8:00 the date will still 28-Apr-2013. After that, date will continue to 29-Apr-2013.
The code is successfully complete the rule, if time before 8:00. But if I changed my computer time to be 11:00 or etc, it will set yesterday back.
$time = date("G:i");
if ($time >= 8:00)
This comparison is not good. Try numerically like
$time = intval(date("Gi"));
if ($time >= 800)
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;
}
?>