views:

224

answers:

4

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 ?

+1  A: 

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");
Pekka
It's actually a return value of `-1`, hence why it's in 1969.
Amber
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.
Pekka
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.
wallyk
+5  A: 

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 ?

Pascal MARTIN
Good and detailed answer!
Pekka
@Pekka : Thanks :-)
Pascal MARTIN
@Pascal: Thank you Pascal for the detailed answer. I am getting warning mentioning stat failed for filename and my error_reporting is enabledNot 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.
Rachel
A: 

You get this if filemtime cannot find the file.

Shiraz Bhaiji
A: 

You can't use

$t = filemtime(filename.txt);

At a minimum, use something like

$t = filemtime("filename.txt");
wallyk