How to compare two time strings properly?

Experts,

I would like to know the best way to compare two strings which contain a time.

Example:

$test = '10:01am';

if($test < '10:00am'){
echo 'am shift';
}else{
echo 'pm shift';
}

Open in new window


Should I convert the times to a different format before executing the comparison?

Cheers!
evibesmusicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
Since you're dealing with strings, you could just check if string 'am' is in your string $test:

$test = '10:01am';
if (stristr('am', $test))
{
  echo 'am shift';
}
else
{
  echo 'pm shift';
}

Open in new window


But since you're dealing with date strings, I'm sure Ray will suggest his wonderful article soon: be patient to get a higher knowledge level :-)
0
Ray PaseurCommented:
Yes, convert the time strings.  Follow the guidance in this article when dealing with date and time values.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
Ray PaseurCommented:
Example here: http://iconoun.com/demo/temp_evibesmusic.php

<?php // demo/temp_evibesmusic.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);

/* SAMPLE FROM http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28502041.html

$test = '10:01am';

if($test < '10:00am'){
echo 'am shift';
}else{
echo 'pm shift';
}
*/

// TEST DATA
$test = '10:01am';
$time = '10:00am';

// CONVERT TO TIMESTAMPS
$ts_test = strtotime('TODAY ' . $test);
$ts_time = strtotime('TODAY ' . $time);

// COMPARE THE VALUES
if ($ts_test < $ts_time)
{
    echo $test . " IS AM SHIFT";
}
else
{
    echo $test . " IS PM SHIFT";
}

Open in new window

HTH, ~Ray
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evibesmusicAuthor Commented:
Thank you as always Ray.
0
Ray PaseurCommented:
Glad to help.  Once you read the article you will see how easy this can be in PHP!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.