Why do I get 31 Dec 1969 as my last modified filename using filemtime in php?

10,298

Solution 1

This probably means your file was not found, either :

  • because it doesn't exist
  • or because it's not in the currect directory
  • or because you didn't quote its name -- you didn't ^^

1st january 1970 is the date of time "zero" ; and filemtime returns false when there is a problem...

So, 31 Dec 1969 is the date/time of zero... According to your locale, I suppose ; I myself, with this code :

$filemtime = filemtime(filename.txt);
$formated = date('Y-m-d H:i:s', $filemtime);
var_dump($filemtime, $formated);

get this output :

boolean false
string '1970-01-01 01:00:00' (length=19)

false because the file doesnt' exist, and 1970-01-01 at 01:00 because of my locale (I'm in France, at UTC+1 hour)


And note I also get a couple of notices and warnings :

  • Notice: Use of undefined constant filename - assumed 'filename'
  • Notice: Use of undefined constant txt - assumed 'txt'
  • Warning: filemtime() [function.filemtime]: stat failed for filenametxt

Do you have any of those ?
If no : are error_reporting and/or display_errors enabled ?

Solution 2

I had the same problem and solved it by making the file path absolute by concatenating complete physical path before $filename as file last modified date by filemtime($physical_path.$filename) and file size by filesize($physical_path.$file).

Solution 3

January 1, 1970 0:00 is the start of the Unix epoch. Thus, a timestamp of 0, which is the result of a failed filemtime operation, together with (probably) a DST issue, is December 31, 1969. You need to fix your filemtime operation, for example (if your example is not just pseudo-code) by adding quotes to the filename:

filemtime ("filename.txt");
Share:
10,298
Rachel
Author by

Rachel

I am here for learning and I learn different aspects of Computer Science from the Valuable Answers which I get from Stackoverflow Users. Thank you SO Community. I owe my knowledge to you.

Updated on June 15, 2022

Comments

  • Rachel
    Rachel almost 2 years

    I am newbie to php and so please do not mind me asking this question but am really getting confused as to why filemtime(filename.txt)gives me as 31 Dec 1969 as my last modified time ?

  • Amber
    Amber over 14 years
    It's actually a return value of -1, hence why it's in 1969.
  • Pekka
    Pekka over 14 years
    Nope, that's what I first thought too, but filemtime() returns false (=0) on failure. The explanation must have something to do with daylight savings time.
  • Rachel
    Rachel over 14 years
    @Pascal: Thank you Pascal for the detailed answer. I am getting warning mentioning stat failed for filename and my error_reporting is enabled Not sure why filemtime is not getting my file as if I echo it than I get the filename but wrong modified time as 31st Dec, still wondering why it is happening that way.
  • wallyk
    wallyk over 14 years
    It's at the tail end of 1969, adjusted by the number of hours and minutes your timezone is away from UTC, since filemtime converts to the local timezone.