Solved

Need help comparing time intervals.

Posted on 2014-11-07
7
68 Views
Last Modified: 2015-01-27
Experts,

I am attempting to compare two dynamic time intervals.

Based on the result of these comparisons I assign a value accordingly.

I believe that I am on the right track but, I think that my PHP syntax in my comparator IF statements are incorrect because I am unable to successfully evaluate AM, PM, and EVE shifts accordingly.

Can someone look at my IF statements below and let me know if I am doing something wrong...seems that most of my comparisons always evaluate the AM shifts incorrectly.

//HERE ARE SOME EXAMPLE TIME INTERVALS THAT COULD BE COMPARED

//SHOULD EVALUATE TO A 'AM SHIFT'
//Example Start Time: 1899-12-30 08:30:00
//Example End Time: 1899-12-30 12:30:00

//SHOULD EVALUATE TO A 'PM SHIFT'
//Example Start Time: 1899-12-30 12:30:00
//Example End Time: 1899-12-30 15:30:00

//SHOULD EVALUATE TO A 'EVE SHIFT'
//Example Start Time: 1899-12-30 15:30:00
//Example End Time: 1899-12-30 20:30:00

//SHOULD EVALUATE TO A 'AM & PM SHIFT'
//Example Start Time: 1899-12-30 8:30:00
//Example End Time: 1899-12-30 15:30:00

////////////////////////////////////////////

//CLEAN UP JOBSTART AND JOBENDTIMES FOR COMPARRISON
$start_time = str_replace('1899-12-30', '', $employee['JobStartTime']);
$start_time = date("g:ia", strtotime($start_time));
$end_time = str_replace('1899-12-30', '', $employee['JobEndTime']);
$end_time = date("g:ia", strtotime($end_time));

//AN AM SHIFT STARTS BEFORE 10AM
$am_shift_key = '10:00am';
//A PM SHIFT STARTS BEFORE 3PM AND ENDS AFTER 3PM
$pm_shift_key_before = '3:00pm';
$pm_shift_key_after = '3:00pm';
//A EVE SHIFT STARTS AFTER 3PM
$eve_shift_key = '2:59pm';
			
//COMPARE JOBSTART AND END TIMES AND SET SHIFT TYPE TO 1 IF STATEMENT EVALUATES TO TRUE

//AM SHIFT
if((strtotime('TODAY '. $start_time)) < (strtotime('TODAY '. $am_shift_key))){
	$am_shift_type = 'AM SHIFT';
}//END ELSE IF

//PM SHIFT
if((strtotime('TODAY '. $start_time)) < (strtotime('TODAY '. $pm_shift_key_before)) && (strtotime('TODAY '. $end_time)) > (strtotime('TODAY '. $pm_shift_key_after)) ){
	$pm_shift_type = 'PM SHIFT';
}//END ELSE IF

//EVE SHIFT
if((strtotime('TODAY '. $start_time)) >= (strtotime('TODAY '. $eve_shift_key))){
	$eve_shift_type = 'EVE SHIFT';
}//END ELSE IF
//END COMPARE JOBSTART AND END TIMES AND SET SHIFT TYPE TO 1 IF STATEMENT EVALUATES TO TRUE

Open in new window

0
Comment
Question by:evibesmusic
  • 4
  • 2
7 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
'strtotime' is specified to return the seconds since 1970 so I don't think your 1899 dates are going to work.

http://php.net/manual/en/function.strtotime.php
0
 

Author Comment

by:evibesmusic
Comment Utility
@Dave:

I clean up my $start_time and $end_time variables to strip them so they take the following format "00:00am".

Would strtotime() still not work when that is done?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Have a read of this.  There are many ways to bollix it up, but once you've got the right ideas about time without dates, it will work right all the time.  Just don't choose a date that happens to be February 29, or the boundary of daylight savings time.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
"00:00am" will cause you problems where ever Daylight Savings Time is observed.   'strtotime' returns a 'timestamp' which is the number of seconds since 1970.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Looking over the code and comments, it seems that we lack some consolidation of thought about the start and end times of the three shifts.  Please post the following details.

AM shift starts at ____ and ends at ____
PM shift starts at ____ and ends at ____
Eve shift starts at ____ and ends at ____

Thanks, ~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Regarding the values from strtotime(), this quote is from the online man page:

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)

Prior to PHP 5.1.0, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems.

For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
This example is "sort of" right.  I say only "sort of" because someone working more than a standard shift or a split shift may get anomalous results.  The truly correct answer will depend on the rest of your application and what it intends to do with the information about shift-work.  Please look this over and post back with any questions.
http://iconoun.com/demo/temp_evibesmusic.php

<?php // demo/temp_evibesmusic.php
error_reporting(E_ALL);
echo '<pre>';


// See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28553320.html


function get_shifts($a, $z)
{
    // AM SHIFT RUNS FROM 8AM TO 2:59PM
    $am = range( (int)800,   (int)1499 );

    // PM SHIFT RUNS FROM 3PM TO 9:59PM
    $pm = range( (int)1500,  (int)2199 );

    // EVE SHIFT RUNS FROM 10PM TO 7:59AM
    $ev1 = range( (int)2200, (int)2399 );
    $ev2 = range( (int)0,    (int)799  );

    // NORMALIZE THE HOURS AND MINUTES
    $hra = (int)date('Hi', strtotime($a));
    $hrz = (int)date('Hi', strtotime($z));

    // DETERMINE WHAT SHIFTS THESE TIMES ARE IN
    $out = array();

    if (in_array($hra, $am))  $out['AM'] = 'AM';
    if (in_array($hrz, $am))  $out['AM'] = 'AM';

    if (in_array($hra, $pm))  $out['PM'] = 'PM';
    if (in_array($hrz, $pm))  $out['PM'] = 'PM';

    if (in_array($hra, $ev1)) $out['EV'] = 'EV';
    if (in_array($hrz, $ev1)) $out['EV'] = 'EV';
    if (in_array($hra, $ev2)) $out['EV'] = 'EV';
    if (in_array($hrz, $ev2)) $out['EV'] = 'EV';

    return array_values($out);
}

// TEST CASES
$tests = array
( array('a' => '1899-12-30 08:30:00', 'z' => '1899-12-30 12:30:00')
, array('a' => '1899-12-30 12:30:00', 'z' => '1899-12-30 15:30:00')
, array('a' => '1899-12-30 15:30:00', 'z' => '1899-12-30 20:30:00')
, array('a' => '1899-12-30 08:30:00', 'z' => '1899-12-30 15:30:00')
, array('a' => '1899-12-30 18:30:00', 'z' => '1899-12-30 23:30:00')
, array('a' => '1899-12-30 23:30:00', 'z' => '1899-12-30 06:30:00')
)
;

foreach ($tests as $test)
{
    echo PHP_EOL;
    print_r($test);
    $x = get_shifts($test['a'], $test['z']);
    echo PHP_EOL;
    print_r($x);
    echo PHP_EOL;
}

Open in new window

HTH, ~Ray
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now