-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.

5
  • 2
    Maybe this post contains something helpful for you? Commented Mar 27, 2012 at 16:19
  • what's your desired input and output? I don't understand your question Commented Mar 27, 2012 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. Commented Mar 27, 2012 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 Commented Mar 27, 2012 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? Commented Mar 27, 2012 at 16:34

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.