在 PHP 中获取特定时间的 strtotime


get strtotime of specific time in php

我想获取一天/时间的时间戳,例如

17/12/2014 8pm

目前我正在做

$curtime = strtotime(date("Y-m-d H:i:s"));

这给了我当前的时间戳,但我需要在晚上 8 点。

任何帮助都非常感谢。提前谢谢。

如果你试图在晚上 8 点获取今天的时间戳,它实际上比使用 date 简单得多,因为您可以在strtotime中使用相对时间:

$curtime = strtotime('today 8pm');

如果您使用 date 进行测试:

echo date('Y-m-d H:i:s', $curtime); // Returns 2014-12-17 20:00:00

这是一篇关于相对日期格式的完整文章,解释了如何构建正确的相对日期。

最好的方法是使用 date_create_from_format。检查格式的参数,你最终会得到这样的东西:

    $date = date_create_from_format('d/m/Y ga', '17/12/2014 8pm');
    if (!empty($date)) {//returns false if can't create date
        $timestamp = $date->getTimestamp();
        //echo date('d/m/Y H:i:s', $timestamp);
    }