2

We are designing a new sensor that sends some data to a webapplication. We use similar sensors, but they use data channels to divide the value send to the application into different data types.

However, this new sensor will send either data or a 32 bit time, with miliseconds. It doesn't send a identifier bit to see whether the value is data or a timestamp.

Because both the data and timestamp are integer value's, How can i check which one it is?

The timestamp will be

YYYY/MM/DD HH:MI:SS:vv

I already found preg_match, like this:

if (preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $value))

But how do i include the miliseconds? Or is there a better way than a simple if else?

Update

I expect that the date recieved by the application will be in int16. So it might be easier to count the values recieved and handle value 9 and 10 like datetimes..

Marco
  • 75
  • 1
  • 13
  • You could use the DateTime Object : http://stackoverflow.com/questions/14504913/verify-valid-date-using-phps-datetime-class – JazZ May 12 '17 at 07:27
  • Possible duplicate of [Verify valid date using PHP's DateTime class](http://stackoverflow.com/questions/14504913/verify-valid-date-using-phps-datetime-class) – webnoob May 12 '17 at 07:57
  • See my update, i do not think it is a duplicate.. – Marco May 12 '17 at 15:38

3 Answers3

5

You could use the createFromFormat() method from the PHP DateTime object.

You would create a function that checks the method errors :

function is_valid_date($date, $format = 'Y/m/d H:i:s.u')
{
    $d = DateTime::createFromFormat($format, $date);
    $errors = DateTime::getLastErrors();         

    return (
        $errors['warning_count'] == 0 && 
        $errors['error_count'] == 0 &&
        $d !== false
    );
}

Hope it helps.

EDIT :

Added the u (for microseconds) on the default format.

JazZ
  • 4,469
  • 2
  • 20
  • 40
  • Thanks. That might help in the if statement. Will look deeper into that. Then it can just be a `if($value = is_valid_date) then ..... else other datatype`. from the top of my head.- – Marco May 12 '17 at 14:53
  • @Marco Glad it helped. Thanks to validate this answer if it solved your question. Good luck. ; ) – JazZ May 15 '17 at 08:43
  • Yeah, if it was the option i went with, i would. However, we determined that the first 2 16bit values in a message are always the timestamps, so i just made a valuecounter and used a if $value is 1 or 2 then do this, else do that. ;) – Marco May 15 '17 at 13:49
1

You could use this regular expression:

With preg_match_all:

preg_match_all("/(\d{4})\/(\d{2})\/(\d{2})\s(\d{2})\:(\d{2})\:(\d{2})\:(\d{2})/", $input_lines, $output_array);

With preg_match:

preg_match("/(\d{4})\/(\d{2})\/(\d{2})\s(\d{2})\:(\d{2})\:(\d{2})\:(\d{2})/", $input_line, $output_array);

Beware, it only checks for the numbers, the backslashes and the ':' character. It doesn't check if the given numbers are valid.

Once you've run the regular expression, you can then check the output array and see if it's empty or not (if it isn't empty it matched the format)

The regular expression matches this range:

0000/00/00 00:00:00:00
9999/99/99 99:99:99:99
Florian Humblot
  • 1,121
  • 11
  • 29
  • Thanks,I will stil lhave to check how the data will be presented to the application, as i do not know that yet. If it is presented as a int, preg_match probably won't work, then i have to use a count or something to get the last 2 values from a message.(message contains 10 values, last 2 are the timestamps) – Marco May 12 '17 at 14:50
0

I fixed it in a different way:

we determined that the first 2 16bit values in a message are always the timestamps, so i just made a valuecounter and used a if $value is 1 or 2 then do this, else do that. This was a lot easier for me then to check each value.

Marco
  • 75
  • 1
  • 13