[英]about date_format in php
I'm using PHP Version 5.5.15 and running php (on my local windows machine) with Xampp. 我正在使用PHP版本5.5.15并使用Xampp运行php(在我的本地Windows机器上)。
I have troubles with the variable $time. 我有变量$ time的麻烦。 $date and $date2 are working.
$ date和$ date2正在运行。
here's the code: 这是代码:
<?php
//include_once('connect.php')
function currencyquote() {
$from = 'EUR';
$to = 'USD';
$url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='.$from .$to .'=X';
$handle = @fopen($url,'r');
if($handle) {
$result = fgets($handle, 4096);
fclose($handle);
}
$allData = explode(',',$result);
$date = $allData[2]; //ex.: "2/18/2015"
$time = $allData[3]; //ex.: "1:48pm" -> New York local time
$close = $allData[1]; // ex.: 1.3151
echo 'the $result = ' . $result .'<br />';
echo 'the $time = ' .$time. '<br />';
$date2 = date_create_from_format("h:ia","01:50pm");
echo 'the $date2 = ' . date_format($date2, "H:i:s") . "<br />";
$date3 = "01:50pm";
$date=date_create_from_format("h:ia",$date3);
echo 'the $date = ' . date_format($date,"H:i:s") . "<br />";
// this is what i want to use :
$time1 = date_create_from_format("h:ia", $time);
echo 'the $time1 = ' . date_format($time1,"H:i:s") . "<br /><br />"; // this line is line 30 of the code
//with strtotime:
echo 'using \'date\' = ' . date("h:i:s", strtotime($allData[3])) . "<br />";
echo 'using \'date()\': '.date('H:i:s', strtotime($time));
}
currencyquote();
?>
and here are the results of the php-jury: 以下是php-jury的结果:
the $result = "EURUSD=X",1.1372,"2/19/2015","7:20am"
the $time = "7:20am"
the $date2 = 13:50:00
the $date = 13:50:00
Warning: date_format() expects parameter 1 to be DateTimeInterface, boolean given in C:\xampp\htdocs\Nofrafin\php\downloader.php on line 30
the $time1 =
using 'date' = 01:00:00
using 'date()': 01:00:00
The message you get most likely means that $time1
is set to false
. 您获得的消息很可能意味着
$time1
设置为false
。 And the reason from that should be that date_create_from_format()
can't match the date format h:is
with the variable you supply, $time
. 原因应该是
date_create_from_format()
不能匹配日期格式h:is
与您提供的变量, $time
。
Moving further up, you have 进一步向上,你有
$time = $allData[3]; //ex.: "1:48pm"
I tried changing to $time = '1:48pm';
我尝试换到
$time = '1:48pm';
and that worked perfectly. 这非常有效。 So that means you don't get the data you think from the csv.
这意味着你没有从csv获得你想到的数据。
I tried to download the same file as you do and got this back: 我试着像你一样下载同一个文件,然后把它拿回来:
"EURUSD=X",1.1371,"2/19/2015","7:50am"
And that's the reason it doesn't work - $time
is set to "7:50am"
with a trailing newline, not 7:50am
. 这就是它不起作用的原因 -
$time
设置为"7:50am"
,后续换行,而不是7:50am
。 Remove the double quotes (eg $time = trim(str_replace('"', '', $time));
and you should hopefully be fine. :) 删除双引号(例如
$time = trim(str_replace('"', '', $time));
你应该没问题。:)
Firstly: 首先:
For CSV reading use fgetcsv() function, so you will not have to filter quotes from CSV output and there is no need to do explode(). 对于CSV读取使用fgetcsv()函数,因此您不必从CSV输出中过滤引号,也不需要执行explode()。
if($handle) {
$result = fgetcsv($handle, 4096);
fclose($handle);
}
$date = $result[2]; //ex.: "2/18/2015"
$time = $result[3]; //ex.: "1:48pm" -> New York local time
$close = $result[1]; // ex.: 1.3151
... ...
$time1 = date_create_from_format("G:ia", $time);
Secondly: 其次:
You should carefully filter the input string for date_create_from_format function. 您应该仔细过滤date_create_from_format函数的输入字符串。
such as: 如:
$time = trim(str_replace("\"","", $time)); // you will get something like 7:20am
and then: 接着:
$time1 = date_create_from_format("G:ia", $time); // G - for 24-hour format without leading zeroes in your case
but use the first solution (fgetcsv()) anyway 但无论如何使用第一个解决方案(fgetcsv())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.