[wp-trac] [WordPress Trac] #47717: Attached JPEGs potentially have an incorrect 'localized' timestamp
WordPress Trac
noreply at wordpress.org
Wed Jul 17 06:21:03 UTC 2019
#47717: Attached JPEGs potentially have an incorrect 'localized' timestamp
--------------------------+-----------------------------
Reporter: githue | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version: 5.2.2
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
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]`.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/47717>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list