check_date_format()

Description

Check date conforms to "yyyy-mm-dd hh:mm" format or any valid partital of that e.g. yyyy-mm.

Parameters

ColumnTypeDefaultDescription
$date
string string form of the date to check

Return

string

Location

include/metadata_functions.php lines 175 to 210

Definition

 
function check_date_format($date)
{
    global 
$lang;

    if (
is_null($date)) {
        
$date "";
    }

    
// Check the format of the date to "yyyy-mm-dd hh:mm:ss"
    
if (
        (
preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$/"$date$parts))
        
// Check the format of the date to "yyyy-mm-dd hh:mm"
        
|| (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2})$/"$date$parts))
        
// Check the format of the date to "yyyy-mm-dd"
        
|| (preg_match("/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/"$date$parts))
    ) {
        if (!
checkdate($parts[2], $parts[3], $parts[1])) {
            return 
str_replace("%date%"$date$lang["invalid_date_error"]);
        }
        return 
str_replace("%date%"$datecheck_date_parts($parts));
    }

    
// Check the format of the date to "yyyy-mm" pads with 01 to ensure validity
    
elseif (preg_match("/^([0-9]{4})-([0-9]{2})$/"$date$parts)) {
        
array_push($parts'01');
        return 
str_replace("%date%"$datecheck_date_parts($parts));
    }
    
// Check the format of the date to "yyyy" pads with 01 to ensure validity
    
elseif (preg_match("/^([0-9]{4})$/"$date$parts)) {
        
array_push($parts'01''01');
        return 
str_replace("%date%"$datecheck_date_parts($parts));
    }

    
// If it matches nothing return unknown format error
    
return str_replace("%date%"$date$lang["unknown_date_format_error"]);
}

This article was last updated 26th April 2025 14:05 Europe/London time based on the source file dated 21st January 2025 15:20 Europe/London time.