Question

I'm looking for a function that does the inverse of date(). Meaning:

$timestamp = inverse_date($format_string, $date_string);
$d = date($format_string, $timestamp);
if ($d === $date_string) {
    echo 'This is what I want';
}

Problems I've run into so far:

strtotime - guesses format, so it might not be good for all formats

strptime - uses strftime's formatting that is different from date's

SOLUTION:

function inverse_date($format_string, $date_string) {
    $dateTime = date_create_from_format($format_string, $date_string);
    $value_ts = date_format($dateTime, 'U');
    return $value_ts;
}

Thanks to Till Helge Helwig for the link.

Was it helpful?

Solution

function inverse_date($format_string, $date_string) {
    $dateTime = date_create_from_format($format_string, $date_string);
    $value_ts = date_format($dateTime, 'U');
    return $value_ts;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top