I am trying to get the result that which time is greater. I mean there is no date only time.
$open_time = date("g:i A", strtotime($restaurant['open_time']));
$close_time = date("g:i A", strtotime($restaurant['close_time']));
$curren_time = date("h:i");
$restaurant['open_time'] and $restaurant['close_time'] is the result from database. It is coming in 24 hour format like 22:30, 10:20.
What i want exactly.
if(($current_time > $open_time) && ($current_time < $close_time)
{
echo "Opened";
}
else
{
echo "Closed";
}
If current_time is 2:00 AM and open_time is 11:00 AM then the result should echo Closed. And like this if current_time is 1:00 PM and closed_time is 11:00 PM then the result should echo Opened. Hope i explained well. Please ask if you have any doubt in my question.
You can solve thus using 24 hour format time also. But remember you can add date but virtually not from database. You can use to get the greater time
Set correct timezone and should work.
$restaurant = [
'open_time' => '11:00',
'close_time' => '22:00'
];
$timeZone = new DateTimeZone('Europe/Warsaw');
$now = new DateTime('now', $timeZone);
$open = DateTime::createFromFormat('H:i', $restaurant['open_time'], $timeZone);
$close = DateTime::createFromFormat('H:i', $restaurant['close_time'], $timeZone);
$working_now = ($now > $open && $now < $close);
if ($working_now) {
echo 'open';
} else {
echo 'closed';
}
You can play with it in sandbox - uncomment the test line to change current time.
If it's open through midnight you might need additional logic there:
if ($open > $close) {
if ($now > $close) {
$close->modify('+1 day');
} else {
$open->modify('-1 day');
}
}
$working_now = ...
Related
In my student attendance project, in everyday in between 9.45 am to 10.30 am, a teacher should mark their students attendance.
So, a teacher should able to mark their attendance only on that time. Rest of the time students list should be disabled.
Here I am facing an issue to find that server current time reached in between 9.45am to 10.30am everyday.
$now = time();
$today = strtotime('9:50');
//do not know how to find current date with time interval 9.45am and 10.30am
if (($today - $now) > 0) {
$status = "disabled";
} else {
$status = "active";
}
Please help me to find a solution for it.
$current_time = date('h:i:s a'); //now
$start = "9:50 am";
$finish = "10:30 am";
$date1 = DateTime::createFromFormat('H:i a', $current_time);
$date2 = DateTime::createFromFormat('H:i a', $start);
$date3 = DateTime::createFromFormat('H:i a', $finish);
if ($date1 > $date2 && $date1 < $date3)
{
$status = "active";
} else {
$status = "disabled";
}
to test with out waiting for the correct time you can set the $current_time like so:
$current_time = "10:31 am";
I hope its self-explanatory, but if you have any questions, ask. and\or see the dateTime class
I currently have a start and end time in this format '00:00' e.g. H:i. I am currently trying to remove 4 hours from the time and then determine if the new time is the day before, e.g. before 00:00.
An example:
$oldStart = '00:00';
$oldStart = date("H:i", strtotime($oldStart));
$oldEnd = '00:30';
$oldEnd = date("H:i", strtotime($oldEnd));
$newStart = date("H:i", strtotime('-4 hours', $oldStart));
$newEnd = date("H:i", strtotime('-4 hours', $oldEnd));
$dayStart = date("H:i", strtotime('00:00'));
if($newStart < $dayStart) {
echo 'day before<br>';
} else {
echo 'current day<br>';
}
I'm expecting this to echo 'day before' as the hours are now 4 hours behind, however it is echoing 'current day'. I can't see where I'm going wrong...
Just use this:
$old = "4:00";
$dt = new DateTime($old);
$dt->modify("-4 hours");
$new = $dt->format("Y-m-d");
if(date("Y-m-d", strtotime($old)) > $new){
echo "Day before";
} else {
echo "Same day";
}
I need to work out if the current hour is between two other times. For example to check if the time is between 07:00 and 10:00 I could use:
$currentTime = new DateTime('09:00');
$startTime = new DateTime('07:00');
$endTime = new DateTime('10:00');
if ($currentTime->format('H:i') >= $startTime->format('H:i') && $currentTime->format('H:i') <= $endTime->format('H:i')) {
// Do something
}
The problem I have is what happens if the time is 01:00 and I want to check if it's between 22:00 and 07:00. I'm not bothered that that it is another day, just if it falls between those two hours on a 24hr clock. Example 01:00 is between 22:00 and 07:00
22:00, 23:00, 00:00, 01:00, 02:00... 07:00
What I'm trying to achieve in the end is different prices can be set for a service between different times. So I currently loop through each hour working out what price bracket that hour falls into and the changing the price accordingly. If anyone has a more elegant solution for the problem I would be grateful to learn.
UPDATE:
Say I have a rule that says between 10pm and 7am I want to charge double. I loop through each hour from the start time to the end time and check if each hour falls between 22:00 (10pm) and 07:00 (7am) and if so it should be charged double. I want to avoid having to take in to account the date.
<?php
$currentTime = strtotime('1:00');
$startTime = strtotime('22:00');
$endTime = strtotime('7:00');
if (
(
$startTime < $endTime &&
$currentTime >= $startTime &&
$currentTime <= $endTime
) ||
(
$startTime > $endTime && (
$currentTime >= $startTime ||
$currentTime <= $endTime
)
)
) {
echo 'open';
} else {
echo 'clse';
}
No need to use DateTime::format() for your comparisons. DateTime objects are already comparable.
To handle working with time periods that span midnight you will need to change the date so you have an accurate reflection of the actual date.
$currentTime = (new DateTime('01:00'))->modify('+1 day');
$startTime = new DateTime('22:00');
$endTime = (new DateTime('07:00'))->modify('+1 day');
if ($currentTime >= $startTime && $currentTime <= $endTime) {
// Do something
}
I found another way starting from the first and more popular solution that has the problem that doesn't work for me.
I have to write something between 10:00 pm and 06:00 am and the comparison does not work because when i pass midnight the endTime was always > of the start time.
so if i don't mind about the day i solved in this way:
$currentTime = new DateTime();
$startTime = new DateTime('22:00');
$endTime = (new DateTime('06:00'))->modify('+1 day');
if ($currentTime->format("a") == "am") {
// echo "It's a new day <br />";
$currentTime = $currentTime->modify('+1 day');
}
if ($currentTime >= $startTime && $currentTime <= $endTime) {
echo "hello";
}
//get current DateTime
$date = new DateTime('now');
$currentHour = 0;
$currentMinutes = 0;
foreach ($date as $item=>$value ) {
if($item=="date"){
$p = explode(" ",$value)[1];
$hour = explode(":",$p);
$currentHour = $hour[0];
$currentMinutes = $hour[1];
}
}
$returnVO["success"] = ((int)$currentHour<12 || ((int)$currentHour>=22 && (int)$currentMinutes > 0) ) ? false : true;
$hour = (int)date('H');
echo ($hour > 7 && $hour < 22) ? 'Open' : 'Close';
$hour = (int)date('H');
echo ($hour <= 7 || $hour >= 22) ? 'Close':'Open';
I want to compare if the current hour is between the interval 7 PM to 1 AM
for example I want to do
if(time() > strtotime('7PM') && time()< strtotime('1AM'))
{
echo "is between 7PM-1AM"
}
#deceze's comment above explains your problem. Here's an example that specifies 1AM the next day:
$now = new DateTime();
$start = new DateTime('7PM');
$finish = new DateTime('tomorrow 1AM');
if($now > $start && $now < $finish)
{
echo "is between 7PM-1AM";
}
I would like to get the time in seconds (standard time() format) of the previous '4 am'.
My first attempt was: mktime(4,0,0).
This works for every time of the day, except between 00:00 and 04:00, because than it will give the time of the next '4 am'.
So how do I tackle this problem?
$now = new DateTime();
$fourAm = new DateTime('today 4am');
if ($now < $fourAm) {
$time = strtotime('yesterday 4am');
} else {
$time = $fourAm->getTimestamp();
}
Where $time is the timestamp of the "last 4am", regardless of what time it currently is.
Edit:
You could also do this without the DateTime objects, but I love that suite of classes, so I usually find any reason at all to use them. :-P
$now = time();
$fourAm = strtotime('today 4am');
if ($now < $fourAm) {
$time = strtotime('yesterday 4am');
} else {
$time = $fourAm;
}
If the time you have created is in the future, subtract one day.
$date = strtotime('yesterday 4am');