如何在PHP中判断datetime字符串是否具有日期组件


How to tell if a datetime string has a date component in PHP

我再次需要你的帮助,这是我最终想做的:

  • 创建一个DateTime对象,该对象(仅)具有给定格式的日期(到目前为止还可以)
  • 用指定格式的日期时间字符串修改它,可以是日期和时间字符串的单个时间。

示例:

文件名中有一个日期,它是DateTime对象的基日期。文件内部是日期和时间字符串,大多数时候只有时间。但是,如果字符串还包含日期,则必须相应地更新基日期。

这有点难以解释,我希望你能理解。我觉得解决办法是很明显的,但我还是找不到。

干杯!

就用strtotime('yourTimeString') http://uk3.php.net/strtotime

如果能够,它将解析字符串,如果不能,则返回false

正如andrew所说,strtotime是解决方案:

下面是一个例子:

$baseDate = "2013-12-01"; //date in file name
$fullDate = strtotime($timeString, $baseDate); //$timestring is the time or datetime string in the file

这将工作,如果$timeString是时间或datetime字符串,很容易在最后!

再次感谢andrew

在字符串上使用date_parse,然后检查小时键

$dateTimeString = "December 7, 1941"; //your string here
$dateArray = date_parse($dateTimeString);
    if ($dateArray["hour"] == "") {
        echo "The string does not contain a time.";
    } else {
        echo "The string contains a time.";
    }