Solved

Need help comparing time intervals.

Posted on 2014-11-07
7
82 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40429759
'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
ID: 40429773
@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 110

Expert Comment

by:Ray Paseur
ID: 40429779
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40429793
"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 110

Expert Comment

by:Ray Paseur
ID: 40429797
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 110

Expert Comment

by:Ray Paseur
ID: 40429798
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 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40429821
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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