I am using a short hand notation of an if statement to format a field. If the field is an empty string I leave it as an empty string, if not then I am trying to format it to a proper datetime format so it can be inserted into a mysql db. here is my php code
$date = ($date == '') ? date("Y-m-d", strtotime($date)) : $date;
for some reason when the $date string is not empty it is returning it int he format ‘m/d/Y’ example: 04/01/2010
When I pull the code out of the shorthand if
$date = date("Y-m-d", strtotime($date));
print($date);
it is formatted correctly like this ‘Y-m-d’ or 2010-04-01. Does anyone know why this happens? Thanks
I’m taking a bit of a stab in the dark here at what you were trying to accomplish, but you might have more luck with this:
This will attempt to parse any incoming date string, and fail to an empty string if the date was unparsable.
Note that it’s important to do a check on strtotime’s ability to parse a date, because date(“Y-m-d”, strtotime($date)) will return a date somewhere around 1970 if $date doesn’t contain a parsable date.
e.g.:
strtotime can only handle dates that fit in a 32 bit timestamp, which limits it to 1970 – 2038. Additionally, some date formats may be ambiguous.