I’m slowly converting all my sites over from the Unix Timestamp to MySQL Datetime and in the process I need a way to determine which it is so that the sites do not break or crash while working on them. For the most part it is working but every so often, a Unix Timestamp is mis-read as a MySQL Datetime so gives odd results.
One Unix Timestamp in particular that is being mis-read is 1643498282 which is being shown as Saturday, October 7th, 8282 while most if not all others are correct. However, 1643498282 is apparently a valid Timestamp for Saturday, January 29th, 2022. Any ideas how to better detect which date type it is?
The function for detecting the type is below and is being called by the function following it.
// DETERMINE WHETHER TIMESTAMP OR DATETIME
// RETURNS TRUE IF MYSQL DATETIME; OTHERWISE FALSE
function isDateTime($value) {
return (strtotime($value) !== FALSE);
}
// PROVIDES READABLE FORMATTED DATE FOR ON-SCREEN DISPLAY
// WORKS WITH BOTH UNIX TIMESTAMP AND MYSQL DATETIME
function lastUpdated($UpdateDate) {
$DateFormat = "l, F jS, Y";
if ($UpdateDate):
if (isDateTime($UpdateDate)) :
// FOR MYSQL DATETIME FIELD VALUE
$FormattedDate = "Last updated on " . date($DateFormat, strtotime($UpdateDate));
else :
// FOR UNIX TIMESTAMP FIELD VALUE
$FormattedDate = "Last updated on " . date($DateFormat, $UpdateDate);
endif;
else:
$FormattedDate = "Today is " . date($DateFormat);
endif;
return $FormattedDate;
}