Get Zenith value and GMT Offset by IP, latitude and longitude - php

I want to use date_sunset and date_sunrise php's functions but the result is not accurate .. I have the user's IP, Longitude, Latitude and i googled how to calculate Zenith value but found nothing.
And the final value in those functions is GMT Offset that i couldn't get it also.
This is available values that i can get for any client and want to use it to get Zenith and GMT Offset ..
geoiprecord Object (
[country_code] => EG
[country_code3] => EGY
[country_name] => Egypt
[region] => C
[city] => Cairo
[postal_code] =>
[latitude] => 30.0771
[longitude] => 31.2859
[area_code] =>
[dma_code] =>
[metro_code] =>
[continent_code] => AF
[region_name] => Cairo Governorate
[timezone] => Africa/Cairo
)

Just leave the $zenith and $gmt_offset parameters at their defaults. The result of these functions is an integer timestamp which is in terms of UTC. Simply convert the result to the time zone you have in your data "Africa/Cairo".
See this answer for a good example of how to convert timestamp to a specific time zone in PHP.

Related

Order weekly international events saved only with day, hour, minute?

I have weekly international events saved with day, hour, minute, somewhat like this:
Array
(
[0] => Array
(
[event] => Pear festival
[city] => London
[country] => UK
[local-day] => Saturday
[local-time] => 09:00
)
[1] => Array
(
[event] => Apple festival
[city] => New York
[country] => US
[local-day] => Saturday
[local-time] => 10:00
)
[2] => Array
(
[event] => Kiwi festival
[city] => Wellington
[country] => NZ
[local-day] => Saturday
[local-time] => 11:00
)
)
I'm trying to figure out a way to order these chronologically by UTC. I guess this is easy enough if I add the current local time zone to each event before the data is saved. However, countries have summer time and standard time. For example, New Zealand is currently on NZST, which is UTC+13, but after March they return to DST UTC+12.
Anyhow, as things currently stand (January 2021), these events would be ordered:
Array
(
[0] => Array
(
[event] => Kiwi festival
[city] => Wellington
[country] => NZ
[utc-day] => Friday
[utc-time] => 22:00
)
[1] => Array
(
[event] => Pear festival
[city] => London
[country] => UK
[utc-day] => Saturday
[utc-time] => 09:00
)
[2] => Array
(
[event] => Apple festival
[city] => New York
[country] => US
[utc-day] => Saturday
[utc-time] => 14:00
)
)
But after March, the NZ time would be an hour out, and the same for the other countries when they switch to summer time.
Any ideas how to solve this?
I would suggest to add a column in the events table which will store timestamp of your event in UTC no matter this the region event is happening in.
You can use PHP's default DateTime class or widely accepted libraries like Carbon to get the UTC timestamp from a date. You already have the countries and the time in the table. So when you save the event, generate its UTC timestamp and save. And for already existing events, you can do that in bulk once.
This way you can just order your events directly by the UTC timestamps column. This will also help you a lot in longer run if you want to get all events which will happen on a specific date and time, you can check that directly using the UTC timestamp column.
On a side note, this is also helpful in a way that you would have no date logic in your MySQL query other than the one to order results.

How to convert date/time in multilingual Cakephp 3 website

I have a multilingual website in both English and Danish.
Everything works fine, but when I am on the Danish version the date/time is incorrect.
Can you please help me how can I get correct date/time in Danish the translation.
[birth_time] => Cake\I18n\FrozenTime Object
(
[time] => 2017-06-27T11:20:00+00:00
[timezone] => UTC
[fixedNowTime] =>
)
Danish? Well I think that is Denmark, in bootstrap in config, maybe you should config:
date_default_timezone_set('Europe/Copenhagen');
or time zone that you using should be:
[birth_time] => Cake\I18n\FrozenTime Object
(
[time] => 2017-06-27T11:20:00+00:00
[timezone] => UTC+01:00
[fixedNowTime] =>
)
now for set or get should create instances:
use Cake\I18n\Time;
// Create from a string datetime.
$time = Time::createFromFormat(
'Y-m-d H:i:s',
$datetime,
'Europe/Copenhagen'
)

Remove array index based on date

I am working with Symfony framework and I have a Controller whose job is to fetch the RSS feeds.
Once I have the fetched feeds in a set of array, I would like to remove those feeds whose date is greater than the date I provide.
So this is the array of object that I get when I fetch the feeds
Array
(
[0] => AppBundle\Entity\Rss Object
(
[id:AppBundle\Entity\Rss:private] =>
[feedItemTitle:AppBundle\Entity\Rss:private] => Project Manager, Investments Business Management - Prudential - Madison, NJ
[feedItemDescription:AppBundle\Entity\Rss:private] => Globally, PREI has offices in Munich, Frankfurt, London, Paris, Luxembourg, Singapore, Seoul, Tokyo, Sydney, and Mexico City....
From Prudential - 30 Jul 2015 20:00:31 GMT
- View all Madison jobs
[feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Project-Manager-at-Prudential-in-Madison,-NJ-27eedeae3104d1be
[feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
(
[date] => 2015-07-30 20:00:31.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[1] => AppBundle\Entity\Rss Object
(
[id:AppBundle\Entity\Rss:private] =>
[feedItemTitle:AppBundle\Entity\Rss:private] => Client Services Specialist - Prudential - Madison, NJ
[feedItemDescription:AppBundle\Entity\Rss:private] => Globally, PREI has offices in Munich, Frankfurt, London, Paris, Luxembourg, Singapore, Seoul, Tokyo, Sydney, and Mexico City....
From Prudential - 03 Aug 2015 19:59:34 GMT
- View all Madison jobs
[feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Client-Service-Specialist-at-Prudential-in-Madison,-NJ-51ad596876a01466
[feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
(
[date] => 2015-08-03 19:59:34.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[2] => AppBundle\Entity\Rss Object
(
[id:AppBundle\Entity\Rss:private] =>
[feedItemTitle:AppBundle\Entity\Rss:private] => Manager, Client Services - Prudential - Madison, NJ
[feedItemDescription:AppBundle\Entity\Rss:private] => Globally, PREI has offices in Munich, Frankfurt, London, Paris, Luxembourg, Singapore, Seoul, Tokyo, Sydney, and Mexico City....
From Prudential - 03 Aug 2015 19:59:32 GMT
- View all Madison jobs
[feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Manager-at-Prudential-in-Madison,-NJ-c0452d58384711e7
[feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
(
[date] => 2015-08-03 19:59:32.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[3] => AppBundle\Entity\Rss Object
(
[id:AppBundle\Entity\Rss:private] =>
[feedItemTitle:AppBundle\Entity\Rss:private] => Technical Sales Representative, Cell Culture Products - STEMCELL Technologies Inc - United States
[feedItemDescription:AppBundle\Entity\Rss:private] => We create novel, useful, standardized products of unfailing quality and deliver them to more than 70 countries via our many regional offices plus distribution...
From STEMCELL Technologies Inc - 01 Aug 2015 01:16:36 GMT
- View all jobs
[feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Technical-Sales-Representative-at-STEMCELL-Technologies-in-United-States-f4c10a0e10852686
[feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
(
[date] => 2015-08-01 01:16:36.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[4] => AppBundle\Entity\Rss Object
(
[id:AppBundle\Entity\Rss:private] =>
[feedItemTitle:AppBundle\Entity\Rss:private] => Technical Sales Representative, Cell Separation Products - STEMCELL Technologies Inc - United States
[feedItemDescription:AppBundle\Entity\Rss:private] => We create novel, useful, standardized products of unfailing quality and deliver them to more than 70 countries via our many regional offices plus distribution...
From STEMCELL Technologies Inc - 31 Jul 2015 01:13:35 GMT
- View all jobs
[feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Technical-Sales-Representative-at-STEMCELL-Technologies-in-United-States-b8c62120268afb55
[feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
(
[date] => 2015-07-31 01:13:35.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
[5] => AppBundle\Entity\Rss Object
(
[id:AppBundle\Entity\Rss:private] =>
[feedItemTitle:AppBundle\Entity\Rss:private] => Accounting Intern - Code Corp - Draper, UT
[feedItemDescription:AppBundle\Entity\Rss:private] => Would love someone that is interested in International Accounting processes because this position will work with our China, Singapore & Europe office....
From Code Corp - 31 Jul 2015 21:13:43 GMT
- View all Draper jobs
[feedItemLink:AppBundle\Entity\Rss:private] => http://www.indeed.com/job/Accounting-Intern-at-Code-in-Draper,-UT-0e0aca45e988cc89
[feedItemPubDate:AppBundle\Entity\Rss:private] => DateTime Object
(
[date] => 2015-07-31 21:13:43.000000
[timezone_type] => 1
[timezone] => +00:00
)
)
)
As you will notice date in above array.
I am passing the above array and date (not the date from an array above but the $date that i would like to match the array date with to exclude the feed or not) in a parameter to a function below
public function isValid($feed, $date)
{
foreach ($feed as $item) {
if ($item->getfeedItemPubDate()->date < $date) {
echo $item->getfeedItemPubDate()->date;
echo "<br>";
}
}
}
I am able to get the list of feeds whose date from array are smaller than $date but what I am stuck at is how to remove those feeds from the array ? and then pass back the new set of array back to the controller.
You can remove array item by unset function. To know key of that item change a little foreach loop. As so, for example
public function isValid($feed, $date)
{
foreach ($feed as $key => $item)
if ($item->getfeedItemPubDate()->date < $date)
unset ($feed[$key]);
}
You can also try the array_filter function to which you provide a callback function, in your case something like
$feed = array_filter($feed, function($item) use($date) {
return ($item->getfeedItemPubDate()->date < $date);
});
where $date is a variable you set before
Try:
public function isValid($feed, $date)
{
foreach ($feed as $item) {
if (strtotime($item->getfeedItemPubDate()->date) < strtotime($date)) {
echo $item->getfeedItemPubDate()->date;
echo "<br>";
}
}
}

PHP simulate Day Light Savings

I have some code that handles dates and times. I think it's OK, but want to test that it works when the clocks go forwards for DST.
I can change the timezone using date_default_timezone_set('Europe/London');, and wondered if there is an easy to simulate being in DST without having to wait till the clocks change!
You don't provide details on what you want to test but I assume you have functions to do time-related stuff, e.g.:
function setExpiryTime(DateTime $start, $minutes){
}
The obvious test is to provide input parameters that will make your code cross DST boundaries. You can either find such information in your favourite search engine or run a simple PHP snippet:
<?php
$timezone = new DateTimeZone('Europe/London');
print_r( $timezone->getTransitions(mktime(0, 0, 0, 1, 1, date('Y')), mktime(0, 0, 0, 12, 31, date('Y'))) );
Array
(
[0] => Array
(
[ts] => 1388530800
[time] => 2013-12-31T23:00:00+0000
[offset] => 0
[isdst] =>
[abbr] => GMT
)
[1] => Array
(
[ts] => 1396141200
[time] => 2014-03-30T01:00:00+0000
[offset] => 3600
[isdst] => 1
[abbr] => BST
)
[2] => Array
(
[ts] => 1414285200
[time] => 2014-10-26T01:00:00+0000
[offset] => 0
[isdst] =>
[abbr] => GMT
)
)
Thus you can test:
setExpiryTime(new DateTime('2014-03-30T00:55:00+0000'), 10);
setExpiryTime(new DateTime('2014-10-26T00:55:00+0000'), 10);
you can certainly find out, whether user is in Daylight saving zone or daylight saving is currently in effect.
echo date('I', time());
This returns 0/1; where,
0 = Daylight saving is not in effect.
1 = Daylight saving is in effect.
more details : http://php.net/manual/en/function.date.php
Based on this result you can make conditional code to simulate your output.

Retrieving locale out of timezone_identifier -php

Is there an easy way to retrieve the locale from the timezone_identifier?
For example:
date_default_timezone_set('Europe/Berlin');
The Locale in that case should de_DE.
As deceze said there is no possiblity of this. But you can get location information from timezone. as php.net says
$tz = new DateTimeZone("Europe/Prague");
print_r($tz->getLocation());
print_r(timezone_location_get($tz));
will output
Array
(
[country_code] => CZ
[latitude] => 50.08333
[longitude] => 14.43333
[comments] =>
)
Array
(
[country_code] => CZ
[latitude] => 50.08333
[longitude] => 14.43333
[comments] =>
)
No, because there's no such 1:1 correlation. Take Belgium for example, where they officially speak three languages in the same timezone.

Categories