I am retrieving my date like this in my php query:
DATE_FORMAT(items.r_date, '%M %D, %Y') as r_date
And then displaying it like this, for example:
January 1st, 1934
However, if the year is 1111, I would like to make the date say something custom.
Such as (I know this isn't real code, just trying to explain what I want to do:
if ($row['r_date'] CONTAINS '*1111*') { $r_date = "my custom message"; } else $r_date = $row['r_date'];
Could anyone help me solve this?
Thank you for your time.
A literal implementation of your pseudo-code uses strpos
:
if (false !== strpos($row['r_date'], '1111')) {
$r_date = "my custom message";
} else {
$r_date = $row['r_date'];
}
However, the "only check the last four digits" answer by Dagon, or the alternate one below using substr_compare, may yield fewer false positives:
if (0 === substr_compare(rtrim($row['r_date']), '1111'))
$r_date = "my custom message";
} else {
$r_date = $row['r_date'];
}
Note I've used trim here in the event your date has spurious white-space on the end.
if u only want to check by its year, then :
$mydate = date('Y', strtotime($row['r_date']))
if ($mydate == '1111' or $mydate == '1970') {
echo "something";
}else{
echo "else"
}
note:
Y = for 4 digit year.
y = for 2 digit year.
1970 is the lower year on system, so if your year is lower than 1970 system will make year to 1970 by default.
You can use DateTime Class. I didn't test but it should be something similar, just change the date format:
$date = DateTime::createFromFormat("m-d-Y", "02-01-1111");
if($date->format("Y")=="1111")
echo "your custom message";
i would use substr() to extract the last 4 characters being your year
if (substr($row['r_date'], -4) != '1111') {
$r_date = $row['r_date']; }
else {
$r_date = "my custom message";
}