I have this function that gives me a set of options in a select input.
The options give me times with 5 minute interval.
The problem is when the time is like 23:45, the options will start from 00:10 and loops based on the $j variable.
This is what I want to do in words:
Give me a list of options in 5 minutes interval from $open_time till $close_time.
If the current Time ($timeNow) is greater than the $open_time, set the $open_time to the $timeNow to be shown as first option.
Do this loop only until the $close_time.
I hope this is clear.
Appreciate your help :)
Here is the code:
function selectTimesofDay(){
$output = "";
$now = date('G:i ', time()); // time now
$timeNow = strtotime($now); // strtotime now
$next_five = ceil($timeNow / 300) * 300; // get next 5 minute
// time now rounded to next 10 minute
$round5minNow = date('G:i', strtotime('+15 minutes',$next_five));
$open_time = strtotime('17:00');
$close_time = strtotime('23:59');
// in the middle of working hours, time sets to current
if($timeNow >= $open_time && $timeNow < $close_time){
$open_time = strtotime($round5minNow);
}
$time_diff = round(($close_time - $open_time)/60) ;
if(date('l') == 'Friday'){
$j = ($time_diff/5)+11; // working hours extended untill 1:00 AM
} else{
$j = ($time_diff/5)-1; // working hours untill 12:00 AM
}
for($i = 0; $i <= $j; $i++){
$b = $i*5;
$data = date('l')." - ".date("H:i", strtotime('+'.$b.' minutes', $open_time));
$output .= "<option value=\"{$data}\">";
$output .= $data;
$output .= "</option>";
}
return $output;
}
What you really need is:
function selectTimesOfDay() {
$open_time = strtotime("17:00");
$close_time = strtotime("23:59");
$now = time();
$output = "";
for( $i=$open_time; $i<$close_time; $i+=300) {
if( $i < $now) continue;
$output .= "<option>".date("l - H:i",$i)."</option>";
}
return $output;
}
So what this does is run a loop checking every five-minute interval between opening and closing. Skip it if it is before the curent time, and otherwise add an option.
It's much more efficient than what you were trying to do, and probably easier to understand too.
You can even put this after the loop:
if( $output == "") return "<option disabled>Sorry, we're closed for today</option>";
Also, notice how I left out the value attribute all the time. That's because in the absence of a value, the option's text is used as a value. Thus this solution avoids needless duplication.
Consider taking the hard-coded open and close times out of the function body. The goal with functions is to write code that you can reuse, so if your hours change then you don't have to change with your function, but rather the arguments that are passed to it.
// sample usage: print '<select>'.selectTimesofDay('17:00', '23:59').'</select>';
function selectTimesofDay($start=false, $end=false, $interval='5 minutes'){
$interval = DateInterval::createFromDateString($interval);
$rounding_interval = $interval->i * 60;
$date = new DateTime(
date('Y-m-d H:i', round(strtotime($start) / $rounding_interval) * $rounding_interval)
);
$end = new DateTime(
date('Y-m-d H:i', round(strtotime($end) / $rounding_interval) * $rounding_interval)
);
$opts = array();
while ($date < $end) {
if ($date->getTimestamp() < time()) {
$date->add($interval);
continue;
}
$data = $date->format('l').' - '.$date->format('H:i');
//$opts[] = '<option value="'.$date->getTimestamp().'">'.$data.'</option>'; // < -- pass the timestamp instead of a string?
$opts[] = '<option>'.$data.'</option>';
$date->add($interval);
}
return count($opts) < 1 ?
'<option value="-1">- closed -</option>' :
implode("\n", $opts);
}
Documentation
PHP's DateTime object - http://www.php.net/manual/en/class.datetime.php
PHP's DateInterval object - http://www.php.net/manual/en/dateinterval.format.php
PHP functions - http://www.php.net/manual/en/functions.user-defined.php
PHP function tutorial - http://www.tizag.com/phpT/phpfunctions.php
Related
This question already has answers here:
Print time in 15-minute increments between two times in the same day
(10 answers)
Closed 11 months ago.
I'm trying to echo list of hours.
<?php
$startTime = "9:00:00";
$endTime = "19:00:00";
$st = strtotime($startTime);
$et = strtotime($endTime);
while ($st < $et) {
echo $st = date( "H:i", strtotime('+30 minutes', $st) );
echo '<br>';
}
?>
Inside that list, users will choose their reservation hour. But my while loop works like infinite loop.
Just like the previous answer, you're messing with data types while doing arithmetic.
PHP ships with built-in date time arithmetic through date and time API.
Your code could look much more clear and elegant and less error prone (no strtotime(), conversion, etc)
You could do something like this instead:
<?php
$startTime = DateTime::createFromFormat('H:i:s', '9:00:00');
$endTime = DateTimeImmutable::createFromFormat('H:i:s', '19:00:00');
$interval = new DateInterval('PT30M'); // 30 minutes
$reservation = [];
while ($startTime <= $endTime) {
$reservation[] = $startTime->format('H:i');
$startTime->add($interval);
}
echo join(PHP_EOL, $reservation) . PHP_EOL;
Links:
Date/Time Arithmetic
You're converting $st from a integer (timestamp) with strtotime() to a string with date() so your loop keeps going since while (string < integer)... is always true.
You can do this instead:
while ($st < $et) {
$st += (30 * 60); // Add 30 minutes in seconds
echo date("H:i", $st);
echo '<br>';
}
i have the following code :
$now = date('2018-12-28 23:00:00');
$begintime = new DateTime('22:00');
$endtime = new DateTime('03:00');
if($now >= $endtime && $now <= $begintime){
// not between times
echo "Run";
} else {
// between times
echo "Skip";
}
and the echo is
Skip
because $now is bigger than $begintime that make the output is false
what is the correct way to know if time is not the between the $begintime and $endtime?
I searched all of the relevant issues and I've just wasted 3 days of my life because of this issues, but couldn't find something that even elucidated anything from this forum and google for me. Please help me this issue has already taken an absurd amount of days from my life already and sorry for my english before.. :D
Your code displays enormous amount of misunderstanding of how things are working and it leads to the problem. Please take a look at official documentation, you will see that date() returns string and DateTime is an object. They can't be compared directly, you need to convert them into comparable types beforehand. Also notice that DateTime expects to get a date, not just time. Actually without date being defined your $endtime is actually smaller then $starttime:
$begintime = new DateTime('22:00');
$endtime = new DateTime('03:00');
echo $begintime < $endtime ? 'Yes':'No';
This code snippet will return No.
You need to convert $now to a DateTime and you need to add dates to your start / end time marks. For example:
$now = \DateTime::createFromFormat('Y-m-d H:i:s', '2018-12-28 23:00:00', new \DateTimeZone('UTC'));
I can't provide example of converting start / end time marks because you have not defined how do they actually need to look like.
One solution may be :
$now = "23:00"; // date ('H:i');
$begintime = "22:00";
$endtime = "03:00";
$now_t = DateTime::createFromFormat('H:i', $now);
$begintime_t = DateTime::createFromFormat('H:i', $begintime);
$endtime_t = DateTime::createFromFormat('H:i', $endtime);
if ($now_t > $begintime_t || $now_t < $endtime_t)
{
echo "Skip";
}
else
{
echo "Run";
}
UPDATE
finnaly, i solve my problem..
can someone remove the duplicate tags from my question and find the correct title for this issue? maybe someone who have same issue like me, can search my question and find the answer..
sorry for my english, i still learning..
// this is variable from mysql database
$mysql_start = "2018-12-28 21:45:00"; // its from mysql DATETIME and time show when this script will be run
$begintime = "22:00"; // user can choose when this script stop
$endtime = "20:00"; // user can choose when this script run
$mysql_start = explode(' ', $mysql_start);
$taskdays = $mysql_start[0]; // 2018-12-28
echo $taskhours = $mysql_start[1]; // 21:45:00
echo "<br>";
$taskhours = explode(':',$taskhours);
$taskhours = $taskhours[0]; // 22
echo $begintime = date($begintime);
echo "<br>";
$begintime = explode(':',$begintime);
$begintime = $begintime[0]; // 20
echo "<br>";
echo $endtime = date($endtime);
echo "<br>";
$endtime = explode(':',$endtime);
$endtime = $endtime[0] - 1; // because if endtime is 6, so if its 05:59, the endtime will be end on 05
echo $endtime = str_pad($endtime, 2, '0', STR_PAD_LEFT); // if 6, it will add 0 so it will be 06
echo "<br>";
$jamarray = array("00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23");
$ray = array();
if ($begintime > $endtime){
echo '$begintime is bigger than $endtime<br>';
foreach($jamarray as $ray) {
if($ray >= $begintime or $ray <= $endtime){
//echo '<br>';
//print_r($ray);
$eray[] = $ray;
}
}
$aslinya = array_diff($jamarray,$eray);
print_r($aslinya);
if (in_array($taskhours, $aslinya))
{
echo " <= script run in this time";
}
}else{
echo '$begintime is less than $endtime<br>';
foreach($jamarray as $ray) {
if($ray >= $begintime and $ray <= $endtime){
//echo '<br>';
//print_r($ray);
$eray[] = $ray;
}
}
$aslinya = array_diff($jamarray,$eray);
print_r($aslinya);
if (in_array($taskhours, $aslinya))
{
echo " <= script run in this time";
}
}
I have this time range in array example:
$timerange = array('01:30:00','01:31:00',...........,'02:30:00');
and 2 variable:
$start_time = '01:15:00';
$end_time = '03:29:00';
if($timerange is between $start_time && $end_time)
{
//do it something if yes.....
}
Please help me, its have any ready function to use in PHP? to check on this.
You need not bother with conversions of your time strings to a time type - you can compare the strings as they are:
<?php
$timerange = array('01:30:00', '01:31:00', '01:32:00', '02:30:00');
$start_time = '01:15:00';
$end_time = '03:29:00';
$between = array();
foreach ($timerange as $time)
if ($start_time <= $time && $time <= $end_time) $between[] = $time;
if ($between)
{
echo "These are the times between $start_time and $end_time:\n";
print_r($between);
}
If you like it better, you can replace the foreach loop with array_filter():
$between = array_filter($timerange,
function($time) use ($start_time, $end_time)
{ return $start_time <= $time && $time <= $end_time; }
);
<?php
$timerange = array(strtotime('01:30:00'), strtotime('01:31:00'), strtotime('03:30:00'));
$start_time = strtotime('01:15:00');
$end_time = strtotime('03:29:00');
foreach($timerange as $key => $text_field){
if($timerange[$key] > $start_time && $timerange[$key] < $end_time){
echo "Existing";
}else{
echo "Not Existing";
}
}
?>
See How to check if a date is in a given range?
Edit: As you are looking in a 24 hour range you can pick and random date when constructing your timestamps and your calculations should hold true as all of them are the same date.
return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));
Where all of those are timestamps.
Also look at this PHP check if time falls within range, questioning common solution if you don't want this to depend on the date but just the time.
i have two different break time
default break time
extra break time
here i want to sum of two times and display 12 hrs format
EX :
$default_time = "00:30";
$extra_time = "00:25";
my expected output : 00:55
but now display 01:00
this is my code
$default_time = $work_data->break_time;
$break_time = $work_data->extra_time;
$total_break = strtotime($default_time)+strtotime($break_time);
echo date("h:i",strtotime($total_break));
Here is the function you can calculate total time by passing the arguments to functions.
$hours, $min are supposed variable which is zero
$default_time = "00:30";
$break_time = "00:25";
function calculate_total_time() {
$i = 0;
foreach(func_get_args() as $time) {
sscanf($time, '%d:%d', $hour, $min);
$i += $hour * 60 + $min;
}
if( $h = floor($i / 60) ) {
$i %= 60;
}
return sprintf('%02d:%02d', $h, $i);
}
// use example
echo calculate_total_time($default_time, $break_time); # 00:55
There is one function call to strtotime function too much.
You should leave out the strtotime() call in the last line, as $total_break already is a UNIX timestamp:
$total_break = strtotime($default_time)+strtotime($break_time);
echo date("h:i",$total_break);
The problem is that you're trying to add too specific timestamps, but what you're trying to achieve is adding two durations. So you need to convert those timestamps into durations. For that you need a base, which in your case is 00:00.
$base = strtotime("00:00");
$default_time = $work_data->break_time;
$default_timestamp = strtotime($default_time);
$default_duration = $default_timestamp - $base; // Duration in seconds
$break_time = $work_data->extra_time;
$break_timestamp = strtotime($break_time);
$break_duration = $break_timestamp - $base; // Duration in seconds
$total_break = $default_duration + $break_duration; // 55 min in seconds
// If you want to calculate the timestamp 00:55, just add the base back to it
echo date("H:i", $base + $total_break);
Consider using standard DateTime and DateInterval classes. All you will need is to convert your second variable value to interval_spec format (see http://php.net/manual/en/dateinterval.construct.php for details):
$defaultTime = "00:30";
$breakTime = "PT00H25M"; // Or just 'PT25M'
$totalBreak = (new DateTime($defaultTime))->add($breakTime);
echo $totalBreak->format('H:i');
You could try the following code fragment:
$time1 = explode(":", $default_time);
$time2 = explode(":", $break_time);
$fulltime = ($time1[0] + $time2[0]) * 60 + $time1[1] + $time2[1];
echo (int)($fulltime / 60) . ":" . ($fulltime % 60);
<?php
$time = "00:30";
$time2 = "00:25";
$secs = strtotime($time2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($time)+$secs);
print_r($result);
?>
Use below code you will definitely get your answers.
$default_time = "00:30:00";
$extra_time = "00:25:00";
$secs = strtotime($extra_time)-strtotime("00:00:00");
$result = date("H:i:s A",strtotime($default_time)+$secs);
echo $result;die;
You can modify above code as per your need.
You could try the following:
$default_time = $work_data->break_time;
$date_start = new DateTime($default_time);
$break_time = $work_data->extra_time;
$interval = new DateInterval("PT" . str_replace(":", "H", $break_time) . "M");
$date_end = $date_start->add($interval);
echo $date_end->format("H:i");
Note that this doesn't account for times which span a 24 hour period
Starting with the number 9 and using php, I would like to be able to count up from there, and echo out the next number in increments of 1.
So, number 9, then after 1 month the number would change to 10, then another month 11, then 12 etc., with no maximum number/stop point.
How can I accomplish this? So far I have the below code.
$number = 9;
$output = $number + 1;
echo $output;
Is there a way to set this to increase once a month?
You can do this with the PHP date()-function. This is one example of doing it if you are not dependent on the day of the month, but adding day functionality is possible and should be quit easy.
$startNumber = 9;
$startYear = 2015;
$startMonth = 9;
$currentYear = intval( date( "Y" ) );
$currentMonth = intval( date( "n" ) );
$monthsToAdd = ( ( $currentYear - $startYear ) * 12 )
+ ( $currentMonth - $startMonth );
echo $startNumber + $monthsToAdd;
From your question, I'd say:
$number = 9;
$output = date('n') + $number;
echo $output;
But that depends on what you are trying to accomplish. You can also wrap the number around the date() with a modulo.
However this is nothing random. If you want to create a random number every month like your topic suggests, use the month as the random seed.
srand(date('n'));
$number = rand();
a very inefficient way would be
<?php
function increm($duration){
while ($i<$duration) {
$i++;
}
return true;
}
$number = 9;
$start = time();
$i = 0;
while (1){
increm(3600*24*30);
$i++;
// Do your code
}
?>
this script would have to be run continuously for months.
A better way would be
<?php
$number = 9;
if(!file_exists('date.txt')){
$date=date('n');
file_put_contents( (string)time());
$date = 0;
}
else{
$date= file_get_contents('date.txt');
$date= date()-(int)$date;
$date= floor($date/(24*3600*30));
}
// do whatever you may
?>
But this script would increase it whenever called as the first open date would be stored. Will work forever (till UNIX can timestamp).
for this purpose you have to store the number in the database, compare with current unix timestamp and update it when the new month is reached.
2 database columns: count_month int(10) and next_month int(10) where next_month will contain the unix timestamp of the first day of the next month. you can run it with cronjobs or on production.
<?php
$now = strtotime("now");
$next_month = strtotime("first day of next month");
if ($query = $dbconnect->prepare("SELECT next_month FROM table1")) {
$query->execute();
$query->bind_result($compare_time);
$query->store_result();
$row_count = $query->num_rows;
if ($row_count > 0) {
while ($query->fetch()) {
if ($compare_time < $now) { // you reached the 1th of the next month time to update
if ($query2 = $dbconnect->prepare("UPDATE table1 SET count_month=count_month +1, next_month=?")) {
$query2->bind_param('i', $next_month);
$query2->execute();
$query2->close();
}
}
}
}
$query->free_result();
$query->close();
}
?>