PHP function strtotime ()

I have a file with the following lines. I want to identify rows that have timestamps in less than a week (starting from August 22 for this example).

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('mdY H:i:s') are created using date('mdY H:i:s') .

Why fgets requires 23 characters when it is actually 22.

What am I doing wrong here with strtotime?

What is the best way to parse lines with timestamps less than a week ago.

EDIT: Got his job

 $date = DateTime::createFromFormat('dmY H:i:s', $contents); if(strtotime($date->format('dmY H:i:s')) > strtotime("last Monday")) 

Use the DateTime function with the format dmY or m/d/Y

Thanks for helping all the guys.

+4
source share
5 answers

Use DateTime :: createFromFormat instead. Strtotime () may have problems finding the format used for your date.

 $date = DateTime::createFromFormat('dmY H:i:s A', $contents); 
+4
source

The length parameter fgets() includes a newline character ( \n ), so you need to pass 22 + 1 for this parameter.

Using fread() would be more appropriate for this case.

For strtotime:

strtotime does not recognize your date format, so you need to parse it using another function. strptime can do this:

 strptime('08-29-2011 10:29:25 AM', '%m-%d-%Y %H:%M:%S %p') 
+1
source

To quote on the page on the man page :

Reading ends when a length of 1 byte has been read, in a new line (which is included in the return value) or in EOF (whichever comes first). If no length is specified, it will continue to read from the stream until it reaches the end of the line.

As for the conversation, I would use strptime () as a fixed date format:

 if(strptime($contents, 'mdY H:i:s A') > strtotime("last Monday")) 

If this does not help you, var_dump() type strtotime() and post the results.

0
source

Your dates do not match the strtotime format for returning timestamp :

 $dt = strtotime("08-29-2011 10:29:25 AM"); var_dump($dt); // false 

Update
But this works for me:

 $lm = strtotime('last Monday'); $dt = strtotime('23-08-2011'); if($dt > $lm) { echo "In if"; // output for dates earlier than 23-08 } else { echo "In else"; // output for 23-08 and later } 
0
source

I don't like when strtotime() gets hit by a bus. This is not evil. He has great flexibility, but he can only come in.

$contents just need to be adjusted so that strtotime() understands that you are feeding it in datetime in a European style. You do not even need to change any numbers.

Using:

 if(strtotime(str_replace("/","-",$contents)) > strtotime("last Monday")){ 

This is a simpler fix than instantiating a new object for each iteration.

0
source

Source: https://habr.com/ru/post/1369512/


All Articles