-1

I need check string, whether that string is in format of

date("h:i A") - date("h:i A")

(ex:6:00 AM- 3:00 PM)

How to check string is in correct format or not?

I need a function logic, like if it is not formatted correctly,return false otherwise true

Dhanu K
  • 11,288
  • 6
  • 24
  • 38

4 Answers4

1

It's a bit unclear what you're asking, but I assume it's something like this:

$myString = '6:00 AM - 3:00 PM'
preg_match('/[0-2]?[0-9]:[0-6][0-9]\s(?:AM|PM)\s-\s[0-2]?[0-9]:[0-6][0-9]\s(?:AM|PM)/', $myString, $matches)
print $matches
?> 6:00 AM - 3:00 PM 
Jokab
  • 2,939
  • 1
  • 15
  • 26
0

It is unclear, but you'll need something like this

\d+:\d+\s[AP]M-\s\d+:\d+\s[AP]M
Mr Mystery Guest
  • 1,464
  • 1
  • 18
  • 47
0

You can check it this way:

$str = '6:00 AM - 3:00 PM';

$parts = explode('-', $str, 2);

$format = '%l:%M %p';

var_dump( strptime($parts[0], $format) && strptime($parts[1], $format) );
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0

I found solution as follows for my string

echo preg_match('/^(?:0[1-9]|1[0-2]|[1-9]):[0-5][0-9] (am|pm|AM|PM)( |)-( |)(?:0[1-9]|1[0-2]|[1-9]):[0-5][0-9] (am|pm|AM|PM)$/', "12:00 AM - 12:00 AM");

you can find solution in enter link description here

Dhanu K
  • 11,288
  • 6
  • 24
  • 38