Reputation: 176
I have a string "scbdemo2016-10-21:getlastweekReadBooks4795". How to find out the above string has date in it and to validate the date is current day date.
Upvotes: 0
Views: 936
Reputation: 1217
Here is your code, but it only works with string that has : character, anyways
function checkdata($date, $format)
{
$format=strstr(preg_replace("/[a-zA-Z]+/", '',$format),":",true);
// date_default_timezone_set('UTC');
$d = DateTime::createFromFormat($format, $date);
if($d && $d->format($format) === $date) {
return true;
} else {
return false;
}
}
$data="scbdemo2016-10-21:getlastweekReadBooks4795";
if(checkdata($data,'Y-m-d'))
echo "Yes";
else
echo "NO";
Upvotes: 0
Reputation: 710
Easily done with regular expressions:
$isToday = stringDateToday("scbdemo2016-10-21:getlastweekReadBooks4795");
function stringDateToday($string) {
$reg = "/\d{4}-\d{2}-\d{2}/";
$date = new DateTime();
preg_match($reg, $string, $matches);
if (isset($matches[0])) {
return $matches[0] == $date->format("Y-m-d");
}
return false;
}
stringDateToday()
will return true
if the date in the string is today. The date can be anywhere in the string.
EDIT:
As suggested in the comments, you may want to change new DateTime()
to new DateTime('now', new DateTimeZone('Europe/London'))
to specify timezone.
Upvotes: 1