gpt4 book ai didi

PHP strtotime() 函数

转载 作者:可可西里 更新时间:2023-10-31 22:58:26 24 4
gpt4 key购买 nike

我有一个包含以下行的文件。我想确定时间戳从现在开始不到一周的行(对于本示例,从 8 月 22 日开始)。

log3.txt
28-08-2011 10:29:25 A string
29-08-2011 14:29:25 A new string
20-08-2011 14:29:25 Don’t include

php file

if($file = fopen("/opt/apache2/htdocs/log3.txt", "r"))
{
while(!feof($file))
{
$contents = fgets($file, 23); // This is supposed to be 22.
echo $contents; // With 22 it only returns 08-29-2011 14:29:25 P
if(strtotime($contents) > strtotime("last Monday"))
{
$string1 = fgets($file);
echo "In if $string1";
// Do something
}
else
{
$string1 = fgets($file);
echo "In else $string1"; //Always goes to else statement.
}
}
}

时间戳是用 date('m-d-Y H:i:s') 创建的。

为什么 fgets 需要 23 个字符,而实际上是 22 个字符。

我在这里用 strtotime 做错了什么?

解析时间戳少于一周前的行的最佳方法是什么。

编辑:让它工作

$date = DateTime::createFromFormat('d-m-Y H:i:s', $contents);

if(strtotime($date->format('d-m-Y H:i:s')) > strtotime("last Monday"))

使用 d-m-Y 格式或 m/d/Y 的 DateTime 函数。

感谢大家的帮助。

最佳答案

改用 DateTime::createFromFormat。 strtotime() 可能无法检测日期的使用格式。

$date = DateTime::createFromFormat('d-m-Y H:i:s A', $contents);

关于PHP strtotime() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7232580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com