How do you get the timezone offset text? For example if I pass in:
America/New_York
...I would like to receive back:
-04:00
I created a function called getTimezoneOffsetText
<?php
function getTimezoneOffsetText($timezone){
date_default_timezone_set( "UTC" );
$daylight_savings_offset_in_seconds = timezone_offset_get(timezone_open($timezone), new DateTime());
$mod = $daylight_savings_offset_in_seconds/60 % 60;
$min = abs($mod);
return sprintf('%+03d', $daylight_savings_offset_in_seconds/60/60) . ':' . sprintf('%02d', $min);
}
$offset1 = 'America/New_York';
$offset2 = 'Asia/Kabul';
$offset3 = 'Asia/Kathmandu';
$offset4 = 'Israel';
$offset5 = 'Greenwich';
$offset6 = 'America/Caracas';
echo '<pre>';
echo $offset1 . ":\t" . getTimezoneOffsetText($offset1) . '<br/>';
echo $offset2 . ":\t\t" . getTimezoneOffsetText($offset2) . '<br/>';
echo $offset3 . ":\t\t" . getTimezoneOffsetText($offset3) . '<br/>';
echo $offset4 . ":\t\t\t" . getTimezoneOffsetText($offset4) . '<br/>';
echo $offset5 . ":\t\t" . getTimezoneOffsetText($offset5) . '<br/>';
echo $offset6 . ":\t" . getTimezoneOffsetText($offset6) . '<br/>';
echo '</pre>';
It generates this data:
America/Caracas: -04:30
Asia/Kabul: +04:30
Asia/Kathmandu: +05:45
Israel: +03:00
Greenwich: +00:00
America/New_York: -04:00
Note that it gets half hour and other "non-standard" timezones such as Caracas, Kabul and Kathmandu. I'm curious though if there is already a function that exists that accomplishes this.
Related
I don't know what are the errors of this code because it's suddenly not accurate the showing value to the user like this:
$holidays = array();
$query_holiday = "SELECT * FROM holiday_tbl";
$result = mysqli_query($dbcon, $query_holiday);
while($row_holiday = mysqli_fetch_assoc($result))
{
array_push($row_holiday, $holidays);
}
if(strtotime(date("Y-m-d")) > strtotime($row['due_date']))
{
$currentDate = date("Y-m-d");
$days = (strtotime($currentDate) - strtotime($row['due_date'])) / 86400;
$daysTemp = $days;
for($i=1;$i<$days;$i++)
{
$currentDay = date("D", strtotime("+ ".$i." days"));
$date_loop = date("Y-m-d", strtotime("+ ".$i." days"));
if($currentDay == "Sun" || $currentDay == "Sat")
{
$daysTemp--;
}
else if(in_array($date_loop, $holidays))
{
$daysTemp--;
}
}
echo $daysTemp;
}
The current implementation is rather convoluted. In these cases I find it's always better to start over and try to simplify the logic as much as possible. Here is my take on your problem:
function calculate_days_late($due_date = '', $holidays = array(), $current_date = 'today') {
$days_late = 0;
// Get the timestamps for due date and current date
$current_date_ts = strtotime($current_date);
$due_date_ts = strtotime($due_date);
// If current date is not after due date then not late
if ($current_date_ts <= $due_date_ts) {
return $days_late;
}
$loop_date_ts = $current_date_ts;
while ($loop_date_ts > $due_date_ts) {
// If the looping date is not a weekend or holiday then count it
if ( ! in_array(date('D', $loop_date_ts), array('Sat','Sun'))
&& ! in_array(date('Y-m-d', $loop_date_ts), $holidays)) {
$days_late++;
}
$loop_date_ts = strtotime('-1 day', $loop_date_ts);
}
return $days_late;
}
// Test
echo '2017-09-05 = ' . calculate_days_late('2017-09-05', array(), '2017-09-05') . "\n";
echo '2017-09-06 = ' . calculate_days_late('2017-09-05', array(), '2017-09-06') . "\n";
echo '2017-09-07 = ' . calculate_days_late('2017-09-05', array(), '2017-09-07') . "\n";
echo '2017-09-08 = ' . calculate_days_late('2017-09-05', array(), '2017-09-08') . "\n";
echo '2017-09-09 = ' . calculate_days_late('2017-09-05', array(), '2017-09-09') . "\n";
echo '2017-09-10 = ' . calculate_days_late('2017-09-05', array(), '2017-09-10') . "\n";
echo '2017-09-11 = ' . calculate_days_late('2017-09-05', array(), '2017-09-11') . "\n";
echo '2017-09-12 = ' . calculate_days_late('2017-09-05', array(), '2017-09-12') . "\n";
echo '2017-09-13 = ' . calculate_days_late('2017-09-05', array(), '2017-09-13') . "\n";
Working example: https://eval.in/856018
I have created a page that asks the user for a date and outputs results from an api. The api requires a start date and end date that can be no more than 7days apart. I have set it up so a user can enter a date and the end date will automatically be set for 7 days later.
I am having issues using the date function, It appears now that the code will automatically use todays date before the user can input their choose.
I want the user to be able to choose there date whether it be todays or a future date, I want my api call to wait for users input but not sure how this can be done.
<?php
$startDate = date('Y-m-d', strtotime(isset($_GET['start'])? $_GET['start'] :date('Y-m-d')));
$endDate = date('Y-m-d', strtotime('+7 days', strtotime($startDate)));
if($startDate){
echo "$endDate";
$params = array(
'start_date' => $startDate,
'end_date' => $endDate,
'api_key' => 'coXJeNygdeuxVKs9yJLecWbfuXsY54Wi9gq37HuN'
);
$data = json_decode(callAPI('GET', 'https://api.nasa.gov/neo/rest/v1/feed', $params));
echo "<h1>Near-Earth Object (NEO) Report between " . $params['start_date'] . " and " . $params['end_date'] . "</h1>";
foreach ($data->near_earth_objects as $date => $count) {
echo "<p>" . sizeof($count) . " objects detected on $date</p>";
echo "<ol>";
foreach ($data->near_earth_objects->$date as $near_earth_object) {
echo "<li>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</a><br>";
echo "Estimated Diameter: " . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . "-" . $near_earth_object->estimated_diameter->meters->estimated_diameter_max . " metres<br>";
echo "<ul>";
foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<li>Close approach on " . $close_approach->close_approach_date . " velocity " . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "missing " . $close_approach->orbiting_body . " by " . $close_approach->miss_distance->kilometers . " km</li> ";
}
echo "</ul></li>";
}
echo "</ol>";
}
}
?>
It is nearly at what you wanted to begin with. Just need to add an else to the if statement and update the start/end dates to return false when no date is entered. Note: I also moved the header above the if and added a tertiary condition to display the date if it is already entered so that it is always displayed.
<?php
$startDate = isset($_GET['start']) ? date('Y-m-d', strtotime($_GET['start'] )) : false;
$endDate = $startDate ? date('Y-m-d', strtotime('+7 days', strtotime($startDate))) : false;
$params = array(
'start_date' => $startDate,
'end_date' => $endDate,
'api_key' => 'coXJeNygdeuxVKs9yJLecWbfuXsY54Wi9gq37HuN'
);
echo '<h1>Near-Earth Object (NEO) Report',
( $startDate ? ' between ' . $params['start_date'] . ' and ' . $params['end_date'] . '</h1>' : '</h1>');
if($startDate) {
echo "$endDate";
$data = json_decode(callAPI('GET', 'https://api.nasa.gov/neo/rest/v1/feed', $params));
foreach ($data->near_earth_objects as $date => $count) {
echo "<p>" . sizeof($count) . " objects detected on $date</p>";
echo "<ol>";
foreach ($data->near_earth_objects->$date as $near_earth_object) {
echo "<li>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</a><br>";
echo "Estimated Diameter: " . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . "-" . $near_earth_object->estimated_diameter->meters->estimated_diameter_max . " metres<br>";
echo "<ul>";
foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<li>Close approach on " . $close_approach->close_approach_date . " velocity " . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "missing " . $close_approach->orbiting_body . " by " . $close_approach->miss_distance->kilometers . " km</li> ";
}
echo "</ul></li>";
}
echo "</ol>";
}
} else {
?><form action="" method="GET">
<label for="startdate">Please enter a start date (end date will be 7 days after the start date):</label>
<input id="startdate" type="date" name="start" />
<input type="submit" />
</form><?php
}
I'm trying to show a list of all nearby weather stations.
I have the code:
$json_string = file_get_contents("http://api.wunderground.com/api/8b19ccf6a06c0826/geolookup/conditions/q/Netherlands/Rotterdam.json");
$parsed_json = json_decode($json_string);
$stations = $parsed_json->{'location'}->{'nearby_weather_stations'}->{'pws'}->{'station'};
$count = count($stations);
for($i = 0; $i < $count; $i++)
{
$station = $stations[$i];
if (count($station) > 1)
{
echo "City: " . $station->{'city'} . "\n";
echo "State: " . $station->{'state'} . "\n";
echo "Latitude: " . $station->{'lat'} . "\n";
echo "Longitude: " . $station->{'lon'} . "\n";
}
}
But currently it's not showing anything, i have searched for examples but i couldn't find any solution fot this problem.
Alternatively, you could use a simple foreach to iterate those values. Consider this example:
$json_string = file_get_contents("http://api.wunderground.com/api/8b19ccf6a06c0826/geolookup/conditions/q/Netherlands/Rotterdam.json");
$parsed_json = json_decode($json_string, true); // <-- second parameter to TRUE to use it as an array
$desired_values = $parsed_json['location']['nearby_weather_stations']['pws']['station'];
foreach($desired_values as $key => $value) {
echo "<hr/>";
echo "City: " . $value['city'] . "<br/>";
echo "State: " . $value['state'] . "<br/>";
echo "Latitude: " . $value['lat'] . "<br/>";
echo "Longitude: " . $value['lon'] . "<br/>";
echo "<hr/>";
}
Sample Fiddle
Can you help me there is a problem that is detect by php when execute but I think my code is okay.
The error say "Catchable fatal error: Object of class stdClass could not be converted to string in C:\Program Files\wamp\www\getkongregate.php on line 46"
Website of kongregate xml: here
This is the code I have:
<?php
$xml = simplexml_load_string(file_get_contents('http://www.kongregate.com/games_for_your_site.xml'));
$json = json_encode($xml);
$games = json_decode($json);
$count = 0;
foreach ($games->game as $game) {
echo $game->id . "\n";
echo $game->title . "\n";
echo $game->thumbnail . "\n";
echo $game->launch_date . "\n";
echo $game->category . "\n";
if (array_key_exists('screenshot',$game)) {
for($i=0;$i<=(count($game->screenshot)-1);$i++) {
echo $game->screenshot[$i] . "\n";
}
}
echo $game->flash_file . "\n";
echo $game->width . "\n";
echo $game->height . "\n";
echo $game->description . "\n";
echo $game->instructions . "\n";
echo $game->gameplays . "\n";
echo $game->rating . "\n\n------\n\n";
if ($count == 100) {break;}
$count++;
}
?>
You are retreiving xml.
Why are you parsing it as json?
Use SimpleXmlElement instead and then iterate through xml elements to retreive your data.
$xml = file_get_contents($map_url);
$simpleXmlElement = simplexml_load_string($xml);
var_dump($simpleXmlElement);
Here: :)
$url = 'http://www.kongregate.com/games_for_your_site.xml';
$content = file_get_contents($url)
$xml = simplexml_load_string($content);
$count = 0;
foreach ($xml as $game) {
echo strval($game->id) . PHP_EOL;
echo strval($game->title) . PHP_EOL;
echo strval($game->thumbnail) . PHP_EOL;
echo strval($game->launch_date) . PHP_EOL;
echo strval($game->category) . PHP_EOL;
if (isset($game->screenshot) && is_array($game->screenshot)) {
foreach ($game->screenshot as $screenshot) {
echo strval($screenshot) . "\n";
}
}
echo strval($game->flash_file) . PHP_EOL;
echo strval($game->width) . PHP_EOL;
echo strval($game->height) . PHP_EOL;
echo strval($game->description) . PHP_EOL;
echo strval($game->instructions) . PHP_EOL;
echo strval($game->gameplays) . PHP_EOL;
echo strval($game->rating) . PHP_EOL;
if ($count >= 100) {
break;
}
$count++;
}
When I write this strftime("%b %d, %Y %l:%I %p" ,strtotime($runrows['date'])); it always displays incorrectly (as in it displays 10:10 or 1:01 or 4:04)
Im trying to diplay the date as July 4, 20ll 7:32 pm
How would I fix this?
can you post what you get from $runrows['date']?
http://php.net/manual/en/function.strftime.php
Here are few examples:
// Outputs: 12/28/2002 - %V,%G,%Y = 52,2002,2002
echo "12/28/2002 - %V,%G,%Y = " . strftime("%V,%G,%Y", strtotime("12/28/2002")) . "\n";
// Outputs: 12/30/2002 - %V,%G,%Y = 1,2003,2002
echo "12/30/2002 - %V,%G,%Y = " . strftime("%V,%G,%Y", strtotime("12/30/2002")) . "\n";
// Outputs: 1/3/2003 - %V,%G,%Y = 1,2003,2003
echo "1/3/2003 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/3/2003")) . "\n";
// Outputs: 1/10/2003 - %V,%G,%Y = 2,2003,2003
echo "1/10/2003 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/10/2003")) . "\n";