WordPress.org

Make WordPress Core

Opened 15 months ago

Closed 15 months ago

Last modified 14 months ago

#47717 closed defect (bug) (maybelater)

Attached JPEGs potentially have an incorrect 'localized' timestamp

Reported by: githue Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.2.2
Component: Media Keywords:
Focuses: Cc:

Description

When you upload a JPEG to the WP media library, WP generates a UNIX timestamp for the created_timestamp meta value. I want to use this timestamp to display a "Date Taken" on my photography site, however sometimes the time is off by 10 hours. Other software like Windows and Photoshop show the correct localized time.

WP uses two IPTC fields to generate the timestamp (in /wp-admin/includes/image.php):

$meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] )

The value of the second IPTC field [2#060] can be "141433+1000" or "141433+0000", etc. The result is that the created_timestamp can sometimes be UTC+10 or UTC+0, which makes it pretty unreliable. For example I'm using the timestamp to display a "Date Taken" on my website for thousands of photos, but there's no way to know if the timestamp has already been localized or not. I don't know much about date formats, but I'd expect timestamps to always be UTC+0.

One way to solve this is to strip out the offset from the time string before strtotime(), I've been using the wp_read_image_metadata filter to do this.

Here's a quick test I made using two images with different timezone values (see attached).

<?php
function read_jpeg($path)
{
	getimagesize($path, $info);
	if (isset($info['APP13'])) {
		$iptc = iptcparse($info['APP13']);
		$date = $iptc['2#055'][0];
		$time = $iptc['2#060'][0];
		$timeNoOffset = explode("+", $time)[0];

		$wp_timestamp = strtotime($date . ' ' . $time);
		$utc_timestamp = strtotime($date . ' ' . $timeNoOffset);

		echo "WP: " . date("h:i:s A, D", $wp_timestamp);
		echo " | Excluding offset: " . date("h:i:s A, D", $utc_timestamp);
		echo "<hr />";
		var_dump($iptc);
	}
}
date_default_timezone_set('UTC');
echo date_default_timezone_get();
?>
<hr />
<br />
<?php
// Correct localized time: 12:00:00 AM, Wed
read_jpeg('./date_created+0.jpg');
?>
<hr />
<br />
<?php
// Correct localized time: 02:14:33 PM, Sun
read_jpeg('./date_created+1000.jpg');
?>

I searched for related issues but I dont think they were related to this specific issue with $meta[created_time].

Attachments (2)

date_created+1000.jpg (27.5 KB) - added by githue 15 months ago.
JPEG with +1000 included in IPTC time
date_created+0.jpg (19.4 KB) - added by githue 15 months ago.
JPEG with +0000 included in IPTC time

Download all attachments as: .zip

Change History (5)

@githue
15 months ago

JPEG with +1000 included in IPTC time

@githue
15 months ago

JPEG with +0000 included in IPTC time

#1 @githue
15 months ago

  • Resolution set to maybelater
  • Status changed from new to closed

I need to do some more research on timezone handling, closing for now.

#2 @mikeschroder
15 months ago

Hi @githue! Thanks for the report!

If you still believe there is a bug here, feel free to leave the ticket open while you research.

#3 @desrosj
14 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.