为什么在 php 中构造 for 循环时不能使用查询字符串中的值?
这对我来说似乎是一个奇怪的问题,但 PHP 在构造 for 循环时似乎无法使用从查询字符串传递的变量。
据我所知,不需要解析,来自查询字符串的数据应该是......无论如何都是一个字符串。
尽管事实上我可以在从查询字符串中检索到这些值后打印出这些值,但这不起作用:
$from = $_GET['dealfrom'];
$to = $_GET['dealto'];
$fromDate = $from;
$toDate = $to;
$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);
for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr = date("Y-m-d",$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
//print $currentDateStr.”<br />”;
}
echo "<pre>";
print_r($dateMonthYearArr);
echo "</pre>";
如果我将值显式设置为字符串($fromdate 和 $todate),以准确地设置我在查询中传递的值字符串,那么代码就可以完美运行。
这是 PHP 在某种程度上的限制,还是我只是错过了一些非常明显的东西?为了这件事我一直把头撞在墙上。
This seems like a strange problem to me, but PHP seems incapable of using a variable passed from the query string when constructing a for loop.
As far as i'm aware there's no parsing required, and data coming from the query string should be...a string anyway.
Despite the fact I can print out the values once i've retrieved them from the query string, this does not work:
$from = $_GET['dealfrom'];
$to = $_GET['dealto'];
$fromDate = $from;
$toDate = $to;
$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);
for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr = date("Y-m-d",$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
//print $currentDateStr.”<br />”;
}
echo "<pre>";
print_r($dateMonthYearArr);
echo "</pre>";
If I set the values explicitly to strings ($fromdate and $todate) to exactly what i'm passing in the query string, then the code runs perfectly.
Is this a limitation of PHP in some way, or am I just missing something really obvious? Been banging my head on the wall over this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的代码使用 $_GET 对我来说工作得很好。和其他人一样,我认为这是您从 $_GET 参数获取的字符串的格式问题。
我用这些值测试了它:01/20/2011 - 03/30/2011,我得到了要打印的数组。
确保您清楚地分析了 $_GET 参数包含的内容,并且 strtotime() 希望能够满足您的要求。
Your code worked fine for me using $_GET. Like others, I believe that it is the formatting issue of the string you're getting from the $_GET param.
I tested it with these values: 01/20/2011 - 03/30/2011 and I got the array to print out.
Make sure you clearly analyze what the $_GET param contains and strtotime() will hopefully oblige.
var_dump()
是你最好的朋友。我很确定 PHP 没有任何问题。
另外,strtotime() 可能会很挑剔,注意格式和区域设置(美国的月份和日期互换了)。
var_dump()
is your best friend here.Im pretty sure there is nothing wrong with PHP.
Also,
strtotime()
can be picky, watch for formatting and locale (US has month and day swapped around).首先,我运行了,
我得到了输出,
所以,你知道吗,它似乎对我有用。
虽然它没有产生我期望的数据,但是,它肯定没有表现出您所看到的行为。
Firstly I ran
I got output of
So, you know what, it seemed to work for me.
Although it didnt produce the data I expected, but, it certainly didnt exhibit the behavior you saw.