Converting date to timestamp based on timezone - php

I'm fetching emails using PHP Imap and this is an example of an object:
Array
(
[0] => stdClass Object
(
[subject] => Email Subject
[from] => Sender <sender#domain.com>
[to] => me#domain.com
[date] => Sat, 19 Aug 2017 20:09:33 +1000
[message_id] => <80d657c74967c8dc56138ca9552f0a2e#anyweb.apca.local>
[size] => 1881518
[uid] => 703
[msgno] => 527
[recent] => 0
[flagged] => 0
[answered] => 0
[deleted] => 0
[seen] => 0
[draft] => 0
[udate] => 1503137430
)
)
Although I do have a udate but I wanted to double check if matches my timezone, so I did:
date_default_timezone_set('Australia/Melbourne');
$str = 'Sat, 19 Aug 2017 20:09:33 +1000';
echo strtotime($str); // 1503137373 ??
Even tried:
$date = new DateTime($str, new DateTimeZone('Australia/Melbourne'));
$timestamp = $date->format('U');
echo $timestamp; // 1503137373 ??
So in both cases I get a timestamp that doesn't match the one thats fetched from the mail server, what am I missing here?

udate - labeled by a mailserver
Date, date - labeled by a client
The difference between 'date' and 'udate' seems to be rather more than
just the way they're formatted.
'date' is the date that was written in the headers by the sender's
mail client, and probably bears little to do with reality. It's
dependent on your sender knowing what the correct time is; it could be
out by a few minutes, days, months or even years.
'udate' is the real date that the e-mail hit your IMAP server.
Use 'udate' if you want to do neat stuff like work out how much e-mail
you get sent on a daily basis - or, as I do, how much spam I get.
http://titanic.fauser.edu/php/function.imap-headerinfo.php.htm
From here:
date - The message date as found in its headers
Date - Same as date
udate - mail message date in Unix time

you can do something like this
date_default_timezone_set('Australia/Melbourne');
$script_tz = date_default_timezone_get();
if (strcmp($script_tz, ini_get('date.timezone'))){
echo 'Script timezone differs from ini-set timezone.';
} else {
echo 'Script timezone and ini-set timezone match.';
}

Related

PHP and DST conversion issue

I'm having trouble understanding how my code adapts to DST, as it's incorrect with the recent update. I'm storing a date time in the database based on UTC and then converting it back to the local timezone for display. If PHP is taking DST into account, something else is wrong because all of my stored dates are 1 hour off.
$stored_date = '2016-11-16 12:04:01'; // in UTC
$dateTime = new DateTime($stored_date, new DateTimeZone('UTC'));
$dateTimeZone = new DateTimeZone('America/New_York');
$dateTime->setTimezone($dateTimeZone);
print_r($dateTime);
Last week, before DST ended, this would have printed out 2016-11-16 08:04:01. This week, now that DST has ended, it prints out 2016-11-16 07:04:01. Why the hour difference if PHP is properly handing the DST shift?
It shouldn't matter the server settings (I don't think) because I'm explicitly doing the conversion within PHP, right?
I'm ready to start doing a check with PHP to see if DST is in effect and offsetting the conversion by 1 hour because I can't figure out why that hour isn't being automatically compensated for within the DateTime class.
New York city switches between these time zones:
Winter: EST (Eastern Standard Time) = UTC -5
Summer: EDT (Eastern Daylight Time) = UTC -4
According to timeanddate.com the switch will happen on 6th of November. Thus the result is correct: 12 - 5 = 7
In other words, PHP is perfectly aware of DST, as we can see in the following code:
$dateTime = new DateTime('2016-11-05 12:04:01', new DateTimeZone('UTC'));
$dateTime->setTimezone(new DateTimeZone('America/New_York'));
echo $dateTime->format('r') . PHP_EOL;
$dateTime = new DateTime('2016-11-06 12:04:01', new DateTimeZone('UTC'));
$dateTime->setTimezone(new DateTimeZone('America/New_York'));
echo $dateTime->format('r') . PHP_EOL;
Sat, 05 Nov 2016 08:04:01 -0400
Sun, 06 Nov 2016 07:04:01 -0500
You can inspect the exact information available in your system's time database:
$timeZone = new DateTimeZone('America/New_York');
print_r($timeZone->getTransitions(mktime(0, 0, 0, 1, 1, 2016), mktime(0, 0, 0, 12, 31, 2016)));
Array
(
[0] => Array
(
[ts] => 1451602800
[time] => 2015-12-31T23:00:00+0000
[offset] => -18000
[isdst] =>
[abbr] => EST
)
[1] => Array
(
[ts] => 1457852400
[time] => 2016-03-13T07:00:00+0000
[offset] => -14400
[isdst] => 1
[abbr] => EDT
)
[2] => Array
(
[ts] => 1478412000
[time] => 2016-11-06T06:00:00+0000
[offset] => -18000
[isdst] =>
[abbr] => EST
)
)

Get timezone location from offset

I need to set timezone in php. I have the offset in seconds of the new timezone and I want to set it using date_default_timezone_set. I have no probelm converting the seconds into offset like +0200 but I don't think it's enough.
From what I understood after reading the manual, is that I need to give as parameter something like America\New_York. Is there a way of converting the offset into a specific location?
You actually can't reliably do this. Each named time zone has multiple unique properties that change over time. The current offset is only part of the information. You also have to consider how daylight saving time applies, which could apply differently (or not at all) per time zone. You also have to consider how a single time zone can have a history of different values as they may have changed their offsets or DST rules many times over.
You should also consider that at any given time, several different time zones will be using the same offset. If you just pick one at random, you are ignoring a lot of important details!
Please read the timezone tag wiki.
The other answers here may return a value, but they are all based on false assumptions. A time zone simply can't be represented by a number alone.
You can search the output of DateTimeZone::listAbbreviations() for timezones which match your offset:
function convertTimezoneOffsetToId($offsetSeconds)
{
$ids = array();
foreach (DateTimeZone::listAbbreviations() as $abbrev) {
foreach ($abbrev as $zone) {
if ($zone['offset'] == $offsetSeconds) {
$ids[] = $zone['timezone_id'];
}
}
}
return array_unique($ids);
}
For example:
convertTimezoneOffsetToId(5040);
returns:
Array
(
[0] => Europe/Vilnius
[1] => Europe/Warsaw
)
Edit
As Matt points out, this function doesn't take into account the historical changes in DST.
So here's an improved function which does:
function convertTimezoneOffsetToId($offsetSeconds, $unixTimestamp)
{
$ids = array();
foreach (DateTimeZone::listIdentifiers() as $id) {
$dtz = new DateTimeZone($id);
$trans = $dtz->getTransitions($unixTimestamp, $unixTimestamp);
if ($trans[0]['offset'] == $offsetSeconds) {
$ids[] = $id;
}
}
return array_unique($ids);
}
It uses DateTimeZone::getTransitions() to get each timezone's offset at a particular moment in history.
For example:
convertTimezoneOffsetToId(19800, time());
returns a list of timezones which have an offset of 19800 seconds right now:
Array
(
[0] => Asia/Colombo
[1] => Asia/Kolkata
)
and:
convertTimezoneOffsetToId(19800, gmmktime(0, 0, 0, 1, 1, 2000));
returns a list of timezones which had an offset of 19800 seconds on 1st Jan 2000:
Array
(
[0] => Asia/Kolkata
)
Notice that Asia/Colombo has disappeared, because on 1st Jan 2000 its offset was 21600 seconds.
There is, yes. I'll just quote this excellent answer by uınbɐɥs:
$timezone = '+2:00';
$timezone = preg_replace('/[^0-9]/', '', $timezone) * 36;
$timezone_name = timezone_name_from_abbr(null, $timezone, true); //= Europe/Paris
date_default_timezone_set($timezone_name);
I'd encourage you to take a look at his answer in detail, since there's a bug that you might need to take into account.
EDITED (original answer completely scrapped)
One approach would be to create a map of offset hour strings (such as +0800) to a timezone name that PHP recognizes. It's not 1-to-1, since there are multiple names for each offset. But since you care about the offset and not the name, then your mapping can just choose any of the available timezone names for any given offset.
Since there aren't hundreds and hundreds of timezones, you really only end up with an array of about 35 entries (there are a few timezones at the 30-minute or even 45-minute mark).
Here is a code sample that pretty much does what you need:
$timezones = array(
'-1100' => 'Pacific/Midway',
'-1000' => 'US/Hawaii',
'-0900' => 'US/Alaska',
'-0800' => 'US/Pacific',
'-0700' => 'US/Arizona',
'-0600' => 'America/Mexico_City',
'-0500' => 'US/Eastern',
'-0430' => 'America/Caracas',
'-0400' => 'Canada/Atlantic',
'-0330' => 'Canada/Newfoundland',
'-0300' => 'America/Buenos_Aires',
'-0200' => 'Atlantic/Stanley',
'-0100' => 'Atlantic/Azores',
'-0100' => 'Atlantic/Cape_Verde',
'+0000' => 'Europe/London',
'+0100' => 'Europe/Amsterdam',
'+0200' => 'Europe/Athens',
'+0300' => 'Asia/Baghdad',
'+0330' => 'Asia/Tehran',
'+0400' => 'Europe/Moscow',
'+0430' => 'Asia/Kabul',
'+0500' => 'Asia/Karachi',
'+0530' => 'Asia/Kolkata',
'+0545' => 'Asia/Kathmandu',
'+0600' => 'Asia/Yekaterinburg',
'+0700' => 'Asia/Novosibirsk',
'+0800' => 'Asia/Krasnoyarsk',
'+0800' => 'Asia/Urumqi',
'+0900' => 'Asia/Irkutsk',
'+0930' => 'Australia/Adelaide',
'+1000' => 'Asia/Yakutsk',
'+1000' => 'Australia/Sydney',
'+1100' => 'Asia/Vladivostok',
'+1200' => 'Asia/Magadan'
);
for ($offset_hours = -11; $offset_hours <= 12; $offset_hours++) {
// Convert to a timezone string. For example, 8 => +0800
$offset_string = sprintf("%+03d", $offset_hours) . "00";
date_default_timezone_set($timezones[$offset_string]);
$dt = new DateTime();
print "OFFSET: $offset_hours hours ($offset_string)\n";
print $dt->format(DATE_RFC822) . "\n";
print "\n";
}
The for loop just demonstrates the setting of almost all the different timezones based on an iterating offset (I excluded the 30-minute and 45-minute mark timezones, for simplicity).
Here is an excerpt of the output from running the above code:
OFFSET: -11 hours (-1100)
Tue, 25 Feb 14 04:24:13 -1100
OFFSET: -10 hours (-1000)
Tue, 25 Feb 14 05:24:13 -1000
OFFSET: -9 hours (-0900)
Tue, 25 Feb 14 06:24:13 -0900
...
...
...
OFFSET: -1 hours (-0100)
Tue, 25 Feb 14 14:24:13 -0100
OFFSET: 0 hours (+0000)
Tue, 25 Feb 14 15:24:13 +0000
OFFSET: 1 hours (+0100)
Tue, 25 Feb 14 16:24:13 +0100
...
...
...
OFFSET: 10 hours (+1000)
Wed, 26 Feb 14 02:24:13 +1100
OFFSET: 11 hours (+1100)
Wed, 26 Feb 14 02:24:13 +1100
OFFSET: 12 hours (+1200)
Wed, 26 Feb 14 03:24:13 +1200
Credit goes to #Eugene Manuilov for his StackOverflow answer regarding PHP Timezones, as it meant I did not have to write up the array entirely from scratch.

Convert local date time to UTC date time

When user enters Date and time using 'input type=date name="date"' && 'input type=time name="time"' tags according to his/her timezone,
for eg :- If a user from India, (Asia/Kolkata) timezone entered a date : 1/22/14 and time : 5:30pm , I need to Convert it into UTC time stamp for storing it to DB, To Achive that
I used below code:-
$year=substr($date,0,4);
$month=substr($date,5,2);
$day=substr($date,8);
$hour=substr($time,0,2);
$minute=substr($time,3);
$clientzone=mysql_result(mysql_query("select timezone from c_users_extra where c_id='{$_SESSION['clientid']}'"),0,0); //Fetches the timezone of user
date_default_timezone_set($clientzone);//Setting default timezone to client timezone
$datetime = new DateTime("$year-$month-$day $hour:$minute:00");
$la_time = new DateTimeZone('UTC');
$datetime->setTimezone($la_time);
$values=$datetime->format('Y-m-d H:i:s');
$year=substr($values,0,4);
$month=substr($values,5,2);
$hour=substr($values,11,2);
$minutes=substr($values,14,2);
$seconds=substr($values,17,2);
$timestamp=mktime($hour,$minutes,$seconds,$month,$day,$year);//creating new timestamp from coverted
print_r(getdate($timestamp));//Result : Array ( [seconds] => 0 [minutes] => 30 [hours] => 6 [mday] => 22 [wday] => 3 [mon] => 1 [year] => 2014 [yday] => 21 [weekday] => Wednesday [month] => January [0] => 1390372200 )
//Expected Result : Array ( [seconds] => 0 [minutes] => 0 [hours] => 12 [mday] => 22 [wday] => 3 [mon] => 1 [year] => 2014 [yday] => 21 [weekday] => Wednesday [month] => January [0] => 1390372200 )
Why I get this wrong time stamp ?
Here is an object oriented example:
<?php
$date = '2014-01-22 18:15:00'; // assumed date is formatted correctly
$clientzone = 'America/New_York'; // assumed timezone is a valid one from your SQL
$dateObj = new DateTime($date, new DateTimeZone($clientzone));
echo "Original: " . $dateObj->format('Y-m-d H:i:sP') . "\n";
$dateObj->setTimezone(new DateTimeZone('UTC')); // convert to UTC
echo "Converted: " . $dateObj->format('Y-m-d H:i:sP') . "\n";
echo "Epoch: ".$dateObj->format('U');
?>
You can format that just like the date function.
The $date and $clientzone are assumed to be valid.
Can't you do something simpler like:
$user_time = strtotime($date);
$dateTimeZone = new DateTimeZone($timezone);
// get the offset from server time (UTC)
$tzOffset = $dateTimeZone->getOffset(new DateTime());
$result_time = $user_time - $tzOffset;
Try following function to convert Local date time to UTC date time
function LocaltoUTC($date_to_convert)
{
$local_timestamp = strtotime($date_t);
$UTC_timestamp += ((-(5.5)) * 3600); // 5.5 is UTC timezone or local timezone
$gmt_datetime = gmdate('y-m-d H:i:s', $UTC_timestamp);
return $gmt_datetime;
}
Here is a similar question. Take a look at Adjusting time zone in PHP with DateTime / DateTimeZone
There is a solution and a more comfortable way to do your task.

Two idential datetimes; one shows 12 hour the other 24 hour

I have a form with two datetime fields. The user inputs the date (yyyy-mm-dd) and time (3 boxes; hour, minute, am/pm).
For some reason, the first one isn't getting saved as a 24 hour time.
The following data is the result of entering:
2011-1-1
4:30 PM
I am using strtotime() to convert a string to a datetime format.
$dateOccured = date('Y-m-d H:i:s', strtotime($dateOccurred));
$dateResolved = date('Y-m-d H:i:s', strtotime($dateResolved));
If I use print_r() to look at the results of this, it is showing correctly.
Date Occurred:
[year] => 2011
[month] => 1
[day] => 1
[hour] => 16
[minute] => 30
[second] => 0
[fraction] => 0
[warning_count] => 0
Date Resolved
[year] => 2011
[month] => 1
[day] => 1
[hour] => 16
[minute] => 30
[second] => 0
[fraction] => 0
[warning_count] => 0
Both show the correct time: 16:30 (or 4:30PM). When I look in my database, this is what is shown.
Date Occurred
2011-01-01 04:30:00
Date Resolved
2011-01-01 16:30:00
I know it's a problem with the dateOccurred variable because if I replace it with dateResolved in my query, it gets inserted correctly. What I can't figure out is where the problem is? What am I missing that is causing this?
Thank you.
If it was the database, wouldn't replacing the dateOccurred variable with dateResolved in the query not matter?
Here is the INSERT query
"INSERT INTO
incidents (
incidentNumber, date, itFunction, issue, severity,
owner, dateOccurred, dateResolved, locationsImpacted, businessImpact,
rcaRequired, rcaReceived, rootCause, notes)
VALUES (
'{$incidentNumber}', '{$date}', '{$itFunction}', '{$issue}', '{$severity}',
'{$owner}', '{$dateOccurred}', '{$dateResolved}', '{$locationsImpacted}', '{$businessImpact}',
'{$rcaRequired}', '{$rcaReceived}', '{$rootCause}', '{$notes}')"
They are both datetime fields.
Found the problem...typos! I've been bashing my head against the wall for an hour trying to figure this out! $dateOccured (single R...rrrrrr)! Put this one in the embarrassing column.
I learned that error reporting can be made more useful by using
error_reporting(E_ALL | E_NOTICE)
Thanks to Chronial for that.

Identify date-related text in a longer message

I'm currently writing a script that would extract all the dates from a message and convert them to timestamps. PHP's strtotime (similar to Unix's date -c 'some date') would be perfect for this, as it recognizes all kinds of dates, such as:
5pm today
2010-11-15 16:30
Thursday 8:00
However, I'm having trouble finding those dates in the first place. For example, in the following string,
I'll be there for dinner tomorrow at 9:00pm
I need to isolate "tomorrow at 9:00pm", as that's the part that strtotime recognizes.
Is there a regular expression or something similar that would return me all dates that can be parsed by strtotime?
The only thing I can think of is date_parse. A regular expression that matches any format accepted by strtotime would be huge.
An example of date_parse:
$str = "I'll be there for dinner tomorrow at 9:00pm";
$parsed = date_parse($str);
print_r($parsed);
It would output something like this (I removed the unimportant parts from it to make it the result lighter):
Array
(
[year] =>
[month] =>
[day] =>
[hour] => 21 // 9:00pm
[minute] => 0 // 9:00pm
[second] => 0 // 9:00pm
[fraction] => 0
[warning_count] => 1
[is_localtime] => 1
[zone_type] => 2
[zone] => -540
[is_dst] =>
[tz_abbr] => I
[relative] => Array
(
[year] => 0
[month] => 0
[day] => 1 // tomorrow (would be -1 for yesterday, etc.)
[hour] => 0
[minute] => 0
[second] => 0
)
)
Whether this works for you depends primarily on what your input looks like. If you have more than one instance of a date in your input string, it will not work as expected.
This might not be totally efficient, but should work for any date string that consists of up to 5 words in length. I would write the function, but I think you'll get the idea with the comments below...
$words = explode(' ',$original_string);
// Use the array_chunk() function break up this array into 1-word,
// 2-word, 3-word, and 4-word long substrings of the original string
// Reform them back into strings and pass each one through strtodate()

Categories