1

A database table named users has a column timemarks.

The timemark fields look like this.. 11:00:00, 13:45:00, 17:00:00, 18:25:00 (time-marks vary for each user)

My php script should list the users who have a specified time-mark.

Query: SELECT * FROM ('users') (query result is returned to $users)

Loop:

  1. iterate through users
  2. create array $timemarks_arr for each user by splitting timemarks string using str_getcsv
  3. echo users with specified time-mark value
foreach ($users as $user): 

   $timemarks_arr = str_getcsv($user->timemarks); //split time-marks by comma and create array

    if (in_array("17:00:00", $timemarks_arr)) //users with specified time-mark
    {                               
        echo $entry->username . "<br />";
    }

endforeach;

For some reason it echos only 2 users but there are more with time-mark 17:00:00. Does anyone have any idea why?

1 Answer 1

4

It won't match values like 17:00:00, because there's a leading space.

Try this to strip away all whitespace from the beginning and end of strings:

$timemarks_arr = array_map( "trim", str_getcsv($user->timemarks)); //split time-marks by comma and create array
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! The trim seems to work. To avoid any other issues I'm considering to change the 00:00:00 time format to timestamp strings when adding to the database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.