Get date from earthtool - PHP & XML parsing - php

I found this web service which provides the date time of a timezone. http://www.earthtools.org/timezone-1.1/24.0167/89.8667
I want to call it & get the values like isotime with php.
So I tried
$contents = simplexml_load_file("http://www.earthtools.org/timezone-1.1/24.0167/89.8667");
$xml = new DOMDocument();
$xml->loadXML( $contents );
AND also with
file_get_contents
With file_get_contents it gets only a string of numbers not the XML format. Something like this
1.0 24.0167 89.8667 6 F 20 Feb 2014 13:50:12 2014-02-20 13:50:12 +0600 2014-02-20 07:50:12 Unknown
Nothing worked. Can anyone please help me that how can I get the isotime or other values from that link using PHP?

Everything works):
$url = 'http://www.earthtools.org/timezone-1.1/24.0167/89.8667';
$nodes = array('localtime', 'isotime', 'utctime');
$cont = file_get_contents($url);
$node_values = array();
if ($cont && ($xml = simplexml_load_string($cont))) {
foreach ($nodes as $node) {
if ($xml->$node) $node_values[$node] = (string)$xml->$node;
}
}
print_r($node_values);

Related

PHPSpreadsheet formula not working between dates

I want to calculate difference between 2 datetimes in hours with PHPSpreadsheet. This is how Excel does it:
A1 and A2 cells format is:
This is the result on web:
When I change value through PHPSpreadsheet, I get #VALUE! and different value formatting.
$reader = PhpSpreadsheet\IOFactory::createReader("Xlsx");
$target_file = __DIR__ . '/test.xlsx';
$spreadsheet = $reader->load($target_file);
$spreadsheet->getActiveSheet()->setCellValue('A1', '24.6.2020 12:30');
$writer = new PhpSpreadsheet\Writer\Html($spreadsheet);
$output = $writer->generateHTMLHeader();
$output .= $writer->generateStyles(true);
$output .= $writer->generateSheetData();
$output .= $writer->generateHTMLFooter();
$doc = new DOMDocument();
#$doc->loadHTML($output);
echo $doc->saveHTML();
I also tried with formatting like this 6/24/2020 14:30 but the result was same (#VALUE!)
$spreadsheet->getActiveSheet()->setCellValue('A1', '6/24/2020 14:30');
Anyone got any idea on how this should be done?
In an Excel document, dates are stored as numbers, not strings. So you need to pass the correct number to setCellValue().
PhpSpreadsheet provides the utility method Date::stringToExcel() to convert strings to Excel dates. You can use it like this:
$date = PhpSpreadsheet\Shared\Date::stringToExcel('2020-06-24 12:30');
$spreadsheet->getActiveSheet()->setCellValue('A1', $date);

Why does the sub string not working? Is there way to sub string the result?

I have current module where I need to list all zip files from S3 AWS to the html tables. now I want to sub-string the 2 digit and the last digit of the number. however when i try to var dump the result still same number the substr not working can you help me guys to find out how to solved this?.
Example.
Number: 1150
Result must be: 11 and 50
I will show you guys my sample code that I already created.
$storage = Storage::disk('s3');
$client = $storage->getAdapter()->getClient();
$command = $client->getCommand('ListObjects');
$command['Bucket'] = $storage->getAdapter()->getBucket();
$command['Prefix'] = '11-10-2019';
$result = $client->execute($command);
foreach ($result['Contents'] as $file) {
$base_name = basename($file['Key']);
$trim_1 = str_replace('exp', '', $base_name);
$trim_2 = substr($trim_1, 1, -4);
var_dump($trim_2);
}
My Output is this:
$number = 1150;
$trim1 = substr($number, 0, 2);
$trim2 = substr($number,-2);
echo $trim1;
echo $trim2;
Based on what you ask you can achieve this by doing something like that.
The output is 11 & 50
Since you are using laravel you can use laravel helper for that which is pretty much the same and go for something like:
Str::substr($number, 2);
Str::substr($number, -2);
Don't forget to include the helper at the top of your file:
use Illuminate\Support\Str;

Simplehtmldom - limit content size for get_html?

I'm using simplehtmldom to get the title of some links and wondering if I can limit the size of the downloaded content? Instead of downloading the whole content just the first 20 lines of code to get the title.
Right now I'm using this:
$html = file_get_html($row['current_url']);
$e = $html->find('title', 0);
$title = $e->innertext;
echo $e->innertext . '<br><br>';
thanks
Unless I've missed something, that's not the way file_get_html works. It's going to retrieve the contents of the page.
In other words, it would have to read the entire page in order to find what it's looking for in the next part.
Now, if you were to use:
$section = file_get_contents('http://www.the-URL.com/', NULL, NULL, 0, 444);
You could probably isolate the first 20 lines of html, so long as the page you are getting is always the same from the <!DOCTYPE html> to the </head><body> or <title></title>.
Then you could grab the first 20 lines, or so, again as long as the amount of Head is the same.
Then use:
$html = str_get_html($section);
And then from there use your 'Find'
$html->find('title', 0);
EDIT:
include('simple_html_dom.php');
$the_url = 'http://www.the-URL.com/';
// Read 444 characters starting from the 1st character
$section = file_get_contents($the_url, NULL, NULL, 0, 444);
$html = str_get_html($section);
if (!$e = $html->find('title', 0)) {
// Read 444 characters starting from the 445th character
$section = file_get_contents($the_url, NULL, NULL, 444, 888);
$html = str_get_html($section);
$e = $html->find('title', 0);
}
$title = $e->innertext;
echo $title . '<br><br>';

Include file with variable

I'm building a sales system in PHP, but I want to include a .xml file, based on a variable.
So the variable i've made is
$date = date("y-m-d D")
output: 16-11-18 Fri.
I've load an .xml via:
$xmlDoc = new DOMDocument();
$xmlDoc->load(YourXmlFile);
But I want to replace 'YourXmlFile' with the value $date returns.
Does Anyone knows how this works?
$xmlDoc = new DOMDocument();
$xmlDoc->load($date . '.xml');
The file in this case would be 16-11-18 Fri.xml
You got
$date = date("y-m-d D") output: 16-11-18 Fri
just append ".xml" extension, so file name will be
$filename = $date.".xml";
Then pass $filename into your function
$xmlDoc = new DOMDocument(); $xmlDoc->load($filename);
I'm not sure you want to ask this only but from your question we can understand this only.

PHP escaping hexadecimal characters

I wrote a script that uses file_get_contents() to open files on server, path is delivered by POST. I only had to receive path from POST and use str_replace() to change "%2F" to "/". Everything worked fine until today. Now in some of supported paths php makes unescaped hexadecimal codes that cannot be supplied to file_get_contents().
Example:
2 parameters:
end = wypisZgierz/Łagiewniki Nowe Zachód/koncowe.htm
start = wypisZgierz/Łagiewniki Nowe Zachód/ogolne.htm
source:
start=wypisZgierz%2F%C5%81agiewniki+Nowe+Zach%C3%B3d%2Fogolne.htm&end=wypisZgierz%2F%C5%81agiewniki+Nowe
+Zach%C3%B3d%2Fkoncowe.htm
Start works fine, but end not.
PHP error log:
PHP Warning: file_get_contents(wypisZgierz/\xc5\x81agiewniki Nowe Zach\xc3\xb3d/koncowe.htm): failed to open stream: No such file or directory
*EDIT 1 *
Thanks to rawurldecode() I coul skip my str_replace(), but this doesn't help with original problem.
Real php code:
<?php
session_start();
$start = "";
$end = "";
foreach ($_POST as $key => $value) {
if($key == "start") {
$start = $value;
$start = str_replace("%2F","/",$start);}
if($key == "end") {
$end = $value;
$end = str_replace("%2F","/",$end);
}
}
$tempstart = file_get_contents($start);
$html = $tempstart;
$tempend = file_get_contents($end);
$html = $html.$tempend;
echo $html;
?>
The problem is that for old data it works fine, and now once $start and $end doesn't work, once only $start works. And if i supply start and end with the same value it still shows error with end (with start it might work or not). #pcdoc
the php function rawurldecode should do the trick! But if you posted some real php code it'd be easier to determine your exact problem!

Categories