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.
Related
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);
I have a file containing:
<?php
return '2000-01-01 00:00:00';
?>
and I have this code:
<?php
$oldValue = require 'file.php';
$now = new DateTime();
$handle = fopen('file.php', "w");
fputs($handle, "<?php\nreturn '" . $now->format('Y-m-d H:i:s') . "';");
fclose($handle);
$newValue = require 'file.php';
echo "Old value: $oldValue ";
echo "New value: $newValue ";
?>
The output with PHP 5.3 is:
Old value: 2000-01-01 00:00:00 New value: 2018-03-28 10:33:12
The output with PHP 7.1 is:
Old value: 2000-01-01 00:00:00 New value: 2000-01-01 00:00:00
In the two cases, the string in the file changes.
Can some one help me to update the new value with PHP 7.1?
Note: it's not the real problem. It's just an abstraction of the problem to make things more simple and comprehensible. So please, no lessons of PHP best practices. I just like to get a good response to my question.
Thanks :)
As commented by iainn The issue is that the PHP server is caching the file once it's loaded and is not re-calling the file from disc on the second require, instead calling it from it's memory cache.
As you have stated that:
"the content of the file changes "
then the issue is the new contents are not passed to the script, instead using the memory of the older contents.
Therefore call clearstatcache() to force clear the cached file data. This should be placed after the new data is written to update the file, and before the file is called for a second time.
If this does not work then the file data may be cached elsewhere in its route.
<?php
$oldValue = require 'file.php';
$now = new DateTime();
$handle = fopen('file.php', "w");
fputs($handle, "<?php\nreturn '" . $now->format('Y-m-d H:i:s') . "';");
fclose($handle);
clearstatcache(); // THIS line should help you
$newValue = require 'file.php';
echo "Old value: $oldValue ";
echo "New value: $newValue ";
?>
As also commented by iainn opcache_invalidate()may be a more specific/less general solution for you.
It could be an issue with your file being cached (OPcache) and php returning the same file in both require calls
Can you try modifying opcache settings
opcache.enable = 0
and then testing it ? Also there is
opcache_reset()
could help you but if you running your code from CLI it may not work.
10-01-2014 ===> means 10 jan 2014
i have source code like this:
foreach($report_data['summary'] as $key=>$row) {
$substrdate=substr($row['payment_type'],-16); //i have check the result is 10-01-2014 & 11-01-2014
$stringvar = '10-01-2014';
$date = DateTime::createFromFormat('d-m-Y', $stringvar);
$summary_data_row[] = array('data'=>'<span style="color:'.$color.'">'.$date->format('Y-m-d').'</span>', 'align'=>'right');
$summary_data_row[] = array('data'=>'<span style="color:'.$color.'">'.$row['comment'].'</span>', 'align'=>'right');
}//end of foreach
it runs well '10-01-2014' become '2014-01-10' in string type as what i want ..until i substitute variable $stringvar with $substrdate which has same value -> '10-01-2014' , syntax $date->format('Y-m-d') makes my program blank page. no error shown in logs.
pls help
I also faced the same problem. and my solution was the following code.
$stringvar = '12-01-2014';
$date= date('Y-m-d',strtotime(str_replace("-","/",$stringvar)));
Try this
//$stringvar = '10-01-2014';
//$date = DateTime::createFromFormat('d-m-Y', $stringvar);
Use this instead of above one
$stringvar = '10-01-2014';
$date = date("Y-m-d", strtotime($stringvar));
I use
<?php
$date = '2014-09-01 14:01:52';
$dateObject = new Zend_Date($date);
$tranDate = $dateObject->get(Zend_Date::ATOM);
echo $tranDate;
?>
2014-09-01T14:01:52+00:00
it's correct, but i noticed than several times it returned
2014-01-09T14:01:52+00:00
Does anybody know what's the problem?
The problem may be related to your $locale.
You should be fine doing this
$dateObject = new Zend_Date($date, 'dd/MM/yyyy');
Or, if you want to be "cleaner", instead of writing the string, check out Zend constants for dates here
http://framework.zend.com/manual/1.12/en/zend.date.constants.html
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);