【问题标题】:php date string to timestamp - inverse_date()php 日期字符串到时间戳 - inverse_date()
【发布时间】:2012-03-27 16:17:06
【问题描述】:

我正在寻找一个与 date() 相反的函数。含义:

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

到目前为止我遇到的问题:

strtotime - 猜测格式,因此它可能不适用于所有格式

strptime - 使用不同于日期的 strftime 格式

解决方案:

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;
}

感谢 Till Helge Helwig 提供链接。

【问题讨论】:

  • 也许this post 包含对您有帮助的内容?
  • 您想要的输入和输出是什么?我不明白你的问题
  • 为什么strtotime应该不行?您可以将任何英文格式的日期转换为时间戳,然后时间戳很容易格式化。你可以先证明日期。
  • 我想要的输入是日期字符串和格式字符串(我知道),输出是时间戳。我需要这个来将给定的日期字符串与当前时间戳进行比较,以确定它是否在过去
  • strtotime 猜测格式,所以我不能依赖它。当我给你 01/02/03 或 03-04-05 时,你会怎么想?

标签: php parsing date inverse


【解决方案1】:
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;
}

【讨论】:

    猜你喜欢
    • 2012-10-25
    • 2013-07-29
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    相关资源
    最近更新 更多