I have a datetime value and i need to check whether the entered datetime is in the format dd/mm/yyyy HH:ii P
? How do i accomplish using jquery in PHP ?
Asked
Active
Viewed 232 times
0
-
Does this answer your question? [Correctly determine if date string is a valid date in that format](https://stackoverflow.com/questions/19271381/correctly-determine-if-date-string-is-a-valid-date-in-that-format) – Jacob Mulquin Apr 13 '20 at 08:29
1 Answers
0
EDIT: See better answer here
I'm not sure what you mean by "using jquery in PHP", but you can determine if the datetime entered is a certain format doing something like the following:
<?php
$date = '13/04/2020 16:32 +02:00';
$format = 'd/m/Y H:i P';
function isValidDateFormat($date, $format)
{
return $date === date($format, strtotime(str_replace('/','.', $date)));
}
var_dump($date);
var_dump(date($format, strtotime(str_replace('/','.', $date))));
var_dump(isValidDateFormat($date, $format));
The str_replace
function is required because strtotime
interprets any date with /
s as using American m/d/Y format (a quite annoying "feature")
Output
string(23) "13/04/2020 16:32 +02:00"
string(23) "13/04/2020 16:32 +02:00"
bool(true)

Jacob Mulquin
- 3,458
- 1
- 19
- 22