-1

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.

Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • 2
    Maybe [this post](http://stackoverflow.com/a/2767419/989169) contains something helpful for you? – Till Helge Mar 27 '12 at 16:19
  • what's your desired input and output? I don't understand your question – JKirchartz Mar 27 '12 at 16:20
  • why should strtotime not be ok? you can convert any english format date to a timestamp and the timestamp is easy to format then. You could prove the date first. – F. Müller Mar 27 '12 at 16:22
  • my desired input is a date string, and the format string (that I know), and the output is the timestamp. I need this to compare a given date string with the current timestamp to decide if it's in the past – Gavriel Mar 27 '12 at 16:33
  • strtotime guesses the format, and so I can't depend on it. What would you think when I give you 01/02/03 or 03-04-05? – Gavriel Mar 27 '12 at 16:34

1 Answers1

0
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;
}
Gavriel
  • 18,880
  • 12
  • 68
  • 105