10

I want to add date and time on an image file that I have uploaded. Which way is the best to accomplish this?

Thanks in advance!

Airikr
  • 6,258
  • 15
  • 59
  • 110
  • Do you want it to be part of the image or a record in the db? – Paul Dessert Mar 01 '12 at 00:13
  • I want it to be a part of the image. For example: you single click on the file in Computer and reads the details about the file. I want to set the date and time that's shows in that information, when it was created. – Airikr Mar 01 '12 at 00:15
  • 2
    That is not PHP's job. That is the file's modified time. Your operating system should be creating that metadata in the image file itself. If you want to get that information within PHP, you can use `filemtime`: http://php.net/manual/en/function.filemtime.php – Andrew Ellis Mar 01 '12 at 00:17
  • 2
    Take a look at this, it might help: http://us2.php.net/manual/en/ref.exif.php – Paul Dessert Mar 01 '12 at 00:18
  • Oh. I didn't right clicked on the file and readed the information in Properties :P But does any operating systems add the created date and time for an uploaded file? – Airikr Mar 01 '12 at 00:19

3 Answers3

20

You can use the touch function to set the last modified date:

touch("path/to/my-image-file.jpg", $someTimestamp);

To retrieve this in PHP, use filemtime.

Ry-
  • 218,210
  • 55
  • 464
  • 476
0

Many modern cameras add date and time to EXIF tags in image files. So many photos already have this data. There are PHP functions to read this data: https://www.php.net/manual/en/ref.exif.php

You can also find some libraries to write this data to image. Here is a discussion on that: writing exif data in php I think it is the best and standard way to store date and time of image inside image file. I think most of desktop applications for photo viewing also support exif.

When no exif data is available then OS file creation time can be used. But this could be the time of last file copy.

Victor
  • 750
  • 9
  • 9
0

Any modern operating system records timestamp information when a file is created (and possibly, when modified or accessed).

You can get this information from php using the stat() call. This returns an associative array with several pieces of data. You'd want the 'atime', 'mtime' or 'ctime' elements.

Read here for the official doc with copious examples:

http://php.net/manual/en/function.stat.php

If you have a pointer to the file (if you're currently using it and it's open for example) you can use fstat instead:

http://www.php.net/manual/en/function.fstat.php

Roadmaster
  • 5,297
  • 1
  • 23
  • 21