I'm developing a simple sistem so our employees could save their overtime so, at the end of the month, they're payed for those extra hours they made.
Normal hours are counted as 1 but nightly ones (through 23:00 to 07:00) should be 1,25 hours each.
That said, we're requesting to introduce the day, and the start and end hours. So, I thought I could have an array like this:
$hours= array(
'0' => true, '1' => true, '2' => true, '3' => true, '4' => true, '5' => true, '6' => true,
'7' => false, '8' => false, '9' => false, '10' => false, '11' => false, '12' => false, '13' => false,
'14' => false, '15' => false, '16' => false, '17' => false, '18' => false, '19' => false, '20' => false,
'21' => false, '22' => false, '23' => true
);
So, basically I test if an hour is special or it isn't with a loop like this:
$normals = 0;
$specials = 0;
$hour_since = '23:00:00';
$hour_since_expl = explode(':',$hour_since);
$hour_to = '23:15:00';
$hour_to_expl= explode(':',$hour_to );
$date = $fecha = '2012-03-14';
$datetime_since= strtotime($date ." ".$hour_since );
$datetime_to= ((int) $hour_to_expl[0] > (int) $hour_to_expl[0]) ?
strtotime(date("Y-m-d h:i:s", strtotime($date ." ".$hour_to)) . " +1 day") :
strtotime($date." ".$hour_to);
$difference = $datetime_to - $datetime_since;
$hours_difference = $difference / SECONDS_PER_HOUR; //60*60
for ($i = 0; $i <= $difference; $i++){
$hour = $i + (int) $hour_since_expl [0];
if ($hours[$hour]) //Special hour here... Pay more!
$specials++;
else
$normals++;
}
But the problem is when hours are not exact and you have started somewhere like 22:30 and ended 00:30 where you have 0,5 hours being not special. I've been struggling my mind but I can't find any solution.
Do someone have any ideas?
Edit: More code given.
Let's assume that start time is 15:15 and endtime is 22:45. First, convert start time to nearest HOUR on or after it and endtime to nearest HOUR on or before it. So we get 15:00 and 22:00. Use your loop for these hours and use the differences as follows: if the difference time (15:15-15:00 or 22:45-22:00) is in overtime, then do overtime*numMins/60 else do standard*numMins/60. I'm not familiar with PHP, so had to do this in pseudo code
edit: took a bit of time, but i love intresting chalanges ;D i hope it helps!
$specialStart = 23;
$specialEnd = 7;
$normals = 0;
$specials = 0;
$hour_since = '22:00:00';
$hour_since_expl = explode(':',$hour_since);
$start_time = mktime($hour_since_expl[0], $hour_since_expl[1], $hour_since_expl[2]);
$hour_to = '07:30:00';
$hour_to_expl= explode(':',$hour_to );
$end_time = mktime($hour_to_expl[0], $hour_to_expl[1], $hour_to_expl[2]);
if($end_time < $start_time){
//worked passed minight, add a day
$end_time = $end_time + 86400;
}
$work_time = ( $end_time - $start_time ) / 60;
for($i = 0; $i < $work_time; $i++){
$time = $start_time + ( $i * 60 );
$hour_of_day = date("H", $time);
if($hour_of_day >= $specialStart || $hour_of_day < $specialEnd){
$specials += 60;
}else{
$normals += 60;
}
}
$specials = $specials / 3600;
$normals = $normals / 3600;
echo "specials: ".$specials;
echo "<br/>normals: ".$normals;
I would suggest you to calculate total time in two separate fields.Firstly get normal hours i.e. before 23:00 and then special hours i.e. after 23:00 and before 07:00.
Then you can calculate pay easily.
Related
I am trying to write a script which will display either an open or closed message depending on a businesses operating hours. I tried using the solution found here and adding to this by setting the timezone. However when I try to run this the value of $status is always 'closed' even if the time of day falls within the values in the array.
Here is my code, any advice would be appreciated. Thanks
//setting default timezone
date_default_timezone_set('Europe/Dublin');
//creating array of opening hours
$openingHours = [
'Sun' => ['12:00' => '20:30'],
'Wed' => ['16:00' => '21:00'],
'Thu' => ['16:00' => '21:00'],
'Fri' => ['16:00' => '23:00'],
'Sat' => ['16:00' => '21:00']
];
//current timestamp
$timestamp = time();
//default status
$status = 'closed';
//get time object from timestamp
$currentTime = (new DateTime())->setTimestamp($timestamp);
//loop through time range for current day
foreach ($openingHours[date('D', $timestamp)] as $startTime => $endTime) {
//create time objects from start and end times
$startTime = DateTime::createFromFormat('h:i A', $startTime);
$endTime = DateTime::createFromFormat('h:i A', $endTime);
//check if current time is within range
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
$status = 'open';
break;
}//end off if
}//end off foreach
echo "we are currently :$status";
In your code the currentTime variable is an object so when you try comparing it things aren't going to match. Try this instead which simplifies things a little.
date_default_timezone_set('Europe/Dublin');
//creating array of opening hours
$openingHours = [
'Sun' => ['12:00' => '20:30'],
'Wed' => ['16:00' => '21:00'],
'Thu' => ['16:00' => '21:00'],
'Fri' => ['16:00' => '23:00'],
'Sat' => ['16:00' => '21:00']
];
//default status
$status = 'closed';
$timeNow = date('H:m',time());
//loop through time range for current day
foreach ($openingHours[date('D', time())] as $startTime => $endTime) {
//check if current time is within range
if (($startTime < $timeNow) && ($timeNow < $endTime)) {
$status = 'open';
break;
} //end off if
} //end off foreach
echo "we are currently :$status";
Edit on July 7th:
If you can change you the opening hours array slightly you may avoid multiple calls to date and the foreach loop using the code below.
date_default_timezone_set('Europe/Dublin');
//creating array of opening hours
$openingHours = [
'Sun' => ['Open'=>'12:00','Close' => '20:30'],
'Wed' => ['Open'=>'16:00','Close' => '21:00'],
'Thu' => ['Open'=>'16:00','Close' => '21:00'],
'Fri' => ['Open'=>'16:00','Close' => '23:00'],
'Sat' => ['Open'=>'16:00','Close' => '21:00']
];
$timeNow = date('H:m',time());
$dow = date('D',time());
echo 'We are currently :' . ((($openingHours[$dow]['Open'] < $timeNow) AND
($timeNow < $openingHours[$dow]['Close'])) ? 'open' : 'closed');
I'm calculating difference between 2 datetimes in seconds but I want to group them by hours. It should be like that.
This is how I'm getting difference between 2 dates.
$date_diff = $first->created_at->diffInSeconds($second->created_at);
And For example first time is 09:47:20 and next one is 11:47:50 I need the group them by hour like this.
In 09:00 = 12.40 Minutes
In 10:00 = 60.00 Minutes
In 11:00 = 47.50 Minutes
How can I do that ? And what you'll suggest for this ?
Thanks
You can check out the code below.
Keep in mind that it works as expected if the start time <= the end time.
Also, there's room for improvement, like extracting logic to separate functions or adding trailing/leading zeros where you'd need them, but it should get you started.
$start = '09:47:20';
$end = '11:47:50';
$startTime = Carbon::createFromFormat('H:i:s', $start);
$endTime = Carbon::createFromFormat('H:i:s', $end);
$startHour = $startTime->hour; // 9
$endHour = $endTime->hour; // 11
$hourRange = range($startHour, $endHour); // array(9, 10, 11)
if (count($hourRange) === 1) {
// start and end time share the same hour
$hourRange = array($startHour, $endHour);
}
$lastIndex = count($hourRange) - 1;
$diffGrouped = array();
foreach ($hourRange as $index => $hour) {
if ($index === 0) {
// start hour has the next full hour - calculate the difference
$nextFullHour = $startHour + 1;
$nextFullHourTime = Carbon::createFromFormat('H', $nextFullHour);
$minutes = $startTime->diff($nextFullHourTime)->format('%i.%s');
} else if ($index === $lastIndex) {
// end hour is its own "previous full hour"
$previousFullHourTime = Carbon::createFromFormat('H', $endHour);
$minutes = $endTime->diff($previousFullHourTime)->format('%i.%s');
} else {
// everything between the start and the end hour
$minutes = '60.00';
}
$diffGrouped[] = array(
'hour' => $hour,
'minutes' => $minutes
);
}
dd($diffGrouped);
/*
array:3 [
0 => array:2 [
"hour" => 9
"minutes" => "12.40"
]
1 => array:2 [
"hour" => 10
"minutes" => "60.00"
]
2 => array:2 [
"hour" => 11
"minutes" => "47.50"
]
]
*/
It's all about booking...
I've got 3 time periods:
Period 3 from 01.01.2014 to 29.04.2014 - Price 3 (per day)
Period 2 from 30.04.2014 to 14.06.2014 - Price 2 (per day)
Period 1 from 15.06.2014 to 21.09.2014 - Price 1 (per day)
Period 4 from 22.09.2014 to 29.04.2015 - Price 3 (per day)
I've already made a php calculation that calculates the booked days and pricing for each period. But I can't figure it out how to calculate between two periods.
For example:
Somebody books from 26.01.2014 to 25.04.2014 = 89 days * Price 1
But it gets really hard when somebody books from period 3 to period 1 for... I tried to separate the calculations:
if ($check_in >= '2013-04-30' && $check_out <= '2014-09-21')
{
//Days and price calcs here
// contains Period 2 and Period 1
}
But it doesn't work well...
Do you have any ideas how to make the whole calculation to work perfectly?
I missed something really important.
Here is the structure:
Period 1
if($numberDays == 1)
{
$price = $price1_period1
}
if($numberDays >= 2 && $numberDays <= 3)
{
$price = $price2_period1 * $numberDays;
}
if($numberDays >= 4 && $numberDays <= 6)
{
$price = $price3_period1 * $numberDays;
}
if($numberDays >= 7 && $numberDays <= 14)
{
$price = $price4_period1 * $numberDays;
}
if($numberDays >= 15 && $numberDays <= 29)
{
$price = $price5_period1 * $numberDays;
}
if($numberDays >= 30)
{
$price = $price6_period1 * $numberDays;
}
It's the same for the other periods. Ex.: for period 2 the price for 6 days is $price3_period2.
You could generate a price for each day. Then loop from start date to end date and sum the dayprice to get the total price:
<?php
$oneDay = 24*3600;
$configs = array(
array(
'startTime' => strtotime('2014-01-01'),
'endTime' => strtotime('2014-04-29'),
'price' => 10
),
array(
'startTime' => strtotime('2014-04-30'),
'endTime' => strtotime('2014-06-14'),
'price' => 20
),
array(
'startTime' => strtotime('2014-06-15'),
'endTime' => strtotime('2014-09-21'),
'price' => 30
),
array(
'startTime' => strtotime('2014-09-22'),
'endTime' => strtotime('2015-04-29'),
'price' => 40
),
);
$prices = array();
foreach ($configs as $config)
{
$time1 = $config['startTime'];
$time2 = $config['endTime'];
$price = $config['price'];
while ($time1 <= $time2)
{
$prices[date('Y-m-d', $time1)] = $price;
$time1 += $oneDay;
}
}
/**
* #param $checkIn in format YYYY-mm-dd
* #param $checkOut in format YYYY-mm-dd
*/
function getTotalPrice($checkIn, $checkOut, $prices)
{
$time1 = strtotime($checkIn);
$time2 = strtotime($checkOut);
$price = 0;
while ($time1 <= $time2)
{
$time1 += 24 * 3600;
$price += $prices[date('Y-m-d', $time1)];
}
return $price;
}
echo getTotalPrice('2014-01-04', '2014-01-09', $prices);
First things first, I assume $check_in and $check_out are strings that you get from some form, then you are comparing them with another string, any of them are dates.
What you can do is convert both $check_in and $check_out to Datetime and then do the comparison, example:
// Check In Date
$check_in_date = new Datetime($check_in);
$date_compare_in = new Datetime('2013-04-30');
$diff_in = $check_in_date->diff($date_compare_in);
// Check Out Date
$check_out_date = new Datetime($check_out);
$date_compare_out = new Datetime('2014-09-21');
$diff_out = $check_out_date->diff($date_compare_out);
Now $diff_in is a DateInterval object that you can check for the quantity of days, example, if the hours are greater than 0, the $check_in was later than the compare date, if is less than 0 the $check_in was before.
if($diff_in->h >= 0 and $diff_out->h <= 0){
// We are within this date range.
}
a DateInterval Object has the following structure:
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 0
)
So I have this function, which displays how long it's been since a timestamp.
Right now if it's been let's say 60 minutes, it will go over and show "1 hour ago". If it's been 24 hours it goes over to "1 day ago" and so on. How could I do so that if it's been for example 1 hour, show minutes also?
Like
Event occured 58 minutes ago
3 minutes later
Event occured 1 hour 1 minute ago
26 hours later
Event occured 1 day 2 hour ago
function humanTiming($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.$text.' ago';
}
}
You need to modify your function like this:
function humanTiming($time)
{
$time = time() - $time; // to get the time since that moment
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
$result = '';
$counter = 1;
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
if ($counter > 2) break;
$numberOfUnits = floor($time / $unit);
$result .= "$numberOfUnits $text ";
$time -= $numberOfUnits * $unit;
++$counter;
}
return "{$result}ago";
}
Check result on codepad.org.
PHP 5.2.0 introduced DateTime object, which will provide a better solution for the problem.
I am going to use my answer on another question to show how easy it is, though will slightly edit the code:
$create_time = "2016-08-02 12:35:04";
$current_time="2016-08-02 16:16:02";
$dtCurrent = DateTime::createFromFormat('Y-m-d H:i:s', $current_time);
// to use current timestamp, use the following:
//$dtCurrent = new DateTime();
$dtCreate = DateTime::createFromFormat('Y-m-d H:i:s', $create_time);
$diff = $dtCurrent->diff($dtCreate);
$interval = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");
$interval will return 0 years 0 months 0 days 3 hours 40 minutes 58 seconds
Let's get rid of those zero values now. Good old regex is pretty much useful here:
$interval = preg_replace('/(^0| 0) (years|months|days|hours|minutes|seconds)/', '', $interval);
In the end we get exactly what we need: 3 hours 40 minutes 58 seconds
You can use a progressive calculation method such as:
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
$tokenValues = array();
foreach($tokens as $tokenValue => $tokenName)
{
if($time > $tokenValue)
{
$tokenValues[$tokenName] = floor($time \ $tokenValue);
$time -= $tikenValue;
}
if(count($tokenValues) == 2)
{
break;
}
}
You should then have only 2 items in tokenValues, adapt as you see fit!
You can find Event occurred time and current time difference using diff. For example see the following code
$time1 = new DateTime('2013-11-21 12:59:00'); // Event occurred time
$time2 = new DateTime(date('Y-m-d H:i:s')); // Current time
$interval = $time1->diff($time2);
echo $interval->y . " Year " .$interval->m . " Month " .$interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds <br/>";
I don't have time to explain, but this function works as I tested it. In addition, I add a feature to handle time from the current time and future. If you supply this function with exact current time, it will return 'just now'. If you supply it with time from the past, it will return a string ended with 'ago'. If you supply it with time from the future, it will return a string ended with 'later'. Another feature is automatic singular/plural form for the time unit. Try it yourself!
function timeEllapsedFrom($from) {
$time = time() - $from;
$diff = abs($time);
$tokens = array (
'year' => 31536000,
'month' => 2592000,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1
);
$result = array();
foreach ($tokens as $id => $length) {
$value = floor($diff/$length);
if ($value) $result[] = "$value $id" . ($value > 1 ? 's' : '');
$diff -= $length*$value;
}
if (!count($result)) return 'just now';
return join(', ', $result) . ($time < 0 ? ' later' : ' ago');
}
You can test it with this code:
echo timeEllapsedFrom(time() - 604800 - 7200 - 480 - 24);
and it will output:
1 week, 2 hours, 8 minutes, 24 seconds ago
Hope it helps!
Using DateTime class. Works with both past and future timestamps, and does correct pluralization when needed:
function elapsedTime($time)
{
date_default_timezone_set('UTC');
$given_time = new DateTime("#$time");
$current_time = new DateTime();
$diff = $given_time->diff($current_time);
$timemap = array('y' => 'year',
'm' => 'month',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second');
$timefmt = array();
foreach ($timemap as $prop => $desc) {
if ($diff->$prop > 0) {
$timefmt[] = ($diff->$prop > 1)
? "{$diff->$prop} {$desc}s"
: "{$diff->$prop} $desc";
}
}
return (!$timefmt)
? 'just now'
: ((count($timefmt) > 1)
? $diff->format(sprintf('%s and %s',
implode(', ', array_slice($timefmt, 0, -1)), end($timefmt)))
: end($timefmt))
. (($given_time < $current_time) ? ' ago' : ' later');
}
Testing:
var_dump(elapsedTime(strtotime('1987-11-25 11:40')));
# string(69) "25 years, 11 months, 29 days, 17 hours, 50 minutes and 42 seconds ago"
var_dump(elapsedTime(time()));
# string(8) "just now"
var_dump(elapsedTime(strtotime('2013-11-25')));
# string(41) "18 hours, 29 minutes and 18 seconds later"
I just wondered if anybody can point me in the right direction: I'm looking to make a script whereby the logo on my site changes depending on the date; so for instance a haloween style one soon.
I started off by having 2 arrays, 1 of start dates and 1 of end dates(not sure even if this is the best way!):
<?php
$start_dates = array('01/01' => 'New Years',
'14/02' => 'Valentine Day',
'16/02/2010' => 'Pancake Day',
'17/03' => 'St Patricks Day',
'01/04' => 'April Fools',
'02/04/2010' => 'Easter',
'23/04' => 'St Georges Day',
'11/06/2010' => 'World Cup',
'31/10' => 'Halloween',
'05/11' => 'Guy Fawkes',
'11/11' => 'Armistice Day',
'16/10' => 'Today',
'15/12' => 'Christmas');
$end_dates = array( '08/01' => 'New Years',
'15/02' => 'Valentine Day',
'17/02/2010' => 'Pancake Day',
'18/03' => 'St Patricks Day',
'02/04' => 'April Fools',
'06/04/2010' => 'Easter',
'24/04' => 'St Georges Day',
'12/07/2010' => 'World Cup',
'01/11' => 'Halloween',
'06/11' => 'Guy Fawkes',
'12/11' => 'Armistice Day',
'17/10' => 'Today',
'01/01' => 'Christmas');
?>
Easy so far...the problemis that I need a way of working out if todays date falls between the start date and end date, then changing the image file name.
Its a long shot but I hope someone would be kind enough to help.
Thanks,
B.
like this
$events = array(
'New Year' => '01/01 01/08',
'Pancake Day' => '16/02/2010 17/02/2010',
//etc
);
echo find_event($events, '16/02');
where find_event() is
function mdy2time($date) {
$e = explode('/', $date);
if(count($e) < 3)
$e[] = '2010';
return strtotime("$e[1]-$e[0]-$e[2]");
}
function find_event($events, $date = null) {
$date = is_null($date) ? time() : mdy2time($date);
foreach($events as $name => $range) {
list($start, $end) = explode(' ', $range);
if($date >= mdy2time($start) && $date <= mdy2time($end))
return $name;
}
return null;
}
you should use an array more like this:
$dates = array();
$dates[] = array(
'name' => 'New Years'
'start' = '01/14',
'end' => '01/20',
'style' => 'haloween',
);
$dates[] = array(
//...
);
then you can get the style as follows:
$style='default';
// date as number e.g. 130 (january 30th)
$currDate = date('md',time()) * 1;
foreach ($dates AS $k => $v) {
$tmp = explode("/",$v['start'];
$start = ($tmp[1].$tmp[0])*1;
$tmp = explode("/",$v['end'];
$stop = ($tmp[1].$tmp[0])*1;
if ($start <= $currDate && $currDate < $stop) {
$style=$v['style'];
break;
}
}
echo 'style: '.$style;
Didn't check the code yet, so feel free to correct me if iam wrong.