Php creating hours except lunch hours - php

I'm creating an appointment system. Hours creating automatically 10 by 10 but I need to remove lunch hours these are 12:00 between 13:00 from 24 hour date time system.
$start=strtotime("08:30");
$end=strtotime("17:30");
$now=$start;
while($now <= $end){
echo '<option value="'.date("H:i",$now).':00">'.date("H:i",$now).'</option>';
$now = strtotime('+10 minutes',$now);
}

Just add lunch time:
$start=strtotime("08:30");
$end=strtotime("17:30");
$startLunch=strtotime("12:00");
$endLunch=strtotime("13:00");
$now = $start;
// and then you can use it in your while loop:
while($now <= $end){
if($now < $startLunch || $now > $endLunch)
{
echo '<option value="'.date("H:i",$now).':00">'.date("H:i",$now).'</option>';
}
$now = strtotime('+10 minutes',$now);
}

Try this out:
$start=strtotime("08:30");
$end=strtotime("17:30");
$lunch_start=strtotime("12:00");
$lunch_end=strtotime("13:00");
$now=$start;
while(($now <= $end) && ($now >= $lunch_start || $now <= $lunch_end)){
echo '<option value="'.date("H:i",$now).':00">'.date("H:i",$now).'</option>';$now = strtotime('+10 minutes',$now);
}

Related

find cout of specific day between two date php without using any loop?

I need to find out count of a specific Day between two date.
I can do this by using loop between two date, but if there is difference of 10 years(suppose) between date loop will run 10*365 times.
Is any easier way to do this?
Thanks in advance
function countDays($day, $start, $end)
{
$start = strtotime($start);
$end = strtotime($end);
$datediff = $end - $start;
$days = floor($datediff / (60 * 60 * 24));
//get the day of the week for start and end dates (0-6)
$w = array( date('w', $start ), date('w', $end) );
//get partial week day count
if ($w[0] < $w[1])
{
$partialWeekCount = ($day >= $w[0] && $day <= $w[1]);
}else if ($w[0] == $w[1])
{
$partialWeekCount = $w[0] == $day;
}else
{
$partialWeekCount = ($day >= $w[0]
$day <= $w[1]);
}
//first count the number of complete weeks, then add 1 if $day falls in a partial week.
return intval($days / 7) + $partialWeekCount;
}
Function Call - countDays( 5, "2017-04-14", "2017-04-21")
You are looking for date_diff() function. The date_diff() function returns the difference between two DateTime objects.
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
To find occurrence of specific day (for eg: Sunday). Please note: I have used 7 for Sunday. You can use 1 for Monday, 2 for Tuesday and so on
$cnt = 0;
$start = new DateTime("2013-03-15");
$end = new DateTime("2013-12-12");
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
if ($dt->format('N') == 7)
{
$cnt++;
}
}
echo $cnt;

PHP If an Hour Is Between Two Other Hours

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';

Compare current hour with period of time

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";
}

Create a loop that will increment time by one hour [duplicate]

This question already has answers here:
Print time in 15-minute increments between two times in the same day
(10 answers)
Closed 9 years ago.
I've spent so much time on trying to figure out how to create a loop that will echo time between given $start_time and $end_time incrementing it by one hour in format HH:MM.
For example if i have $start_time = "09:00" and $end_time="15:00" the output should be like:
09:00
10:00
11:00
12:00
13:00
14:00
15:00
Any idea how to do it? I know i should use php timestamps and strtotime etc. by i have never used them, so i can't do anything, pls help...
You can do it with mktime()
$start = 7;
$end = 15;
for ($time = $start; $time <= $end; $time++) {
echo date("H:00", mktime($time+1)).'<br>';
}
EDIT:
Just for fun, here is another example
foreach(range(intval('07:00:00'),intval('16:00:00')) as $time) {
echo date("H:00", mktime($time+1)).'<br>';
}
$interval = date_interval_create_from_date_string('1 hour');
$begin = date_create('09:00');
$end = date_create('15:00')->add($interval);
foreach (new DatePeriod($begin, $interval, $end) as $dt) {
echo $dt->format('H:i') . "\n";
}
Demos:
https://eval.in/56289
https://eval.in/56291
https://eval.in/56292
If you just want to echo the times you can do something simple like this:
$start_time = 9;
$end_time = 15;
for ($time = $start_time; $time <= $end_time; $time++) {
if ($time < 10)
echo "0";
echo $time . ":00\n";
}
$start_time = "15:49";
$hours = 9;
list($h, $m) = explode(':', $start_time);
while ($hours-->0)
{
echo $h++ . ":" . $m . "\n";
}
Its been awhile since I've coded PHP but look at this link.
Note I posted this before the Question was edited.
while($starttime < $endtime)
{
echo $starttime;
$starttime = date_add($starttime, date_interval_create_from_date_string('1 hour'));
}

Add 15 minutes to current time and snap to 15 minute intervals

I need to add 15 minutes to the current time.
For eg : now is 20:48 , need to add 15 minutes, so now it will be 21:03, but i need to set 21:15 ,that is , it should be in multiples of15,30,45,00.
Help help/guidance would be of good help.
<?php
$current_date_time = date('d/m/Y H:i:s');
$current_date = date("d/m/Y H:i", strtotime($current_date_time."+15 minutes"));
echo $current_date;exit;
Here's a simple example
//what time is it?
$t=time();
//how long is our interval?
$interval=15*60;
//we can calculate when the last interval started by subtracting $t % $interval
$last = $t - $t % $interval;
//so now we know when the next interval will start...
$next = $last + $interval;
echo "Next interval at ".strftime('%H:%M:%S', $next)."\n";
You look like you might want to add 2*$interval to the last interval, but you should be able to adapt this to suit you.
Just to correct my post:
$time = time();
$last_time = ($time - ($time % (15 * 60)));
$in15mins = $last_time+ (15 * 60);
echo 'Now: '. date('Y-m-d H:i') ."\n";
echo 'in 15 mins: '. date('Y-m-d H:i', $in15mins) ."\n";
I think it is quite self explaining. Optimize it, use it.
$current_date_time = date('d/m/Y H:i:s');
echo $current_date_time."<br>";
$current_date = date("d/m/Y H:i", strtotime($current_date_time."+15 minutes"));
echo $current_date."<br>";
$minutes = date("i",strtotime($current_date));
$min = '';
if($minutes > 0 && $minutes <15){
$min = 15 - $minutes;
} else if($minutes > 15 && $minutes <30){
$min = 30 - $minutes;
} else if($minutes > 30 && $minutes <45){
$min = 45 - $minutes;
} else {
$min = 59 - $minutes;
$min++;
}
$newdate = date("d/m/Y H:i", strtotime($current_date."+".$min." minutes"));
echo $newdate;
Use the above code. this is working for me.
$currentTimeStamp=strtotime('now');
$nextInterval=date("Y-m-d H:i", strtotime("+15 minutes", $currentTimeStamp));
dump($nextInterval);

Categories