为什么在 php 中构造 for 循环时不能使用查询字符串中的值?

发布于 2024-10-30 05:56:51 字数 866 浏览 3 评论 0原文

这对我来说似乎是一个奇怪的问题,但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

皇甫轩 2024-11-06 05:56:51

你的代码使用 $_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.

心房的律动 2024-11-06 05:56:51

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).

明月夜 2024-11-06 05:56:51

首先,我运行了,

<?
$from = "13/01/2011";
$to = "21/01/2011";

$fromDate = $from;
$toDate = $to;

echo $from."\n";
echo $to."\n";


$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);
echo $fromDateTS."\n";
echo $toDateTS."\n";

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."\n";
//print $currentDateStr..<br />.;
}

echo  "<pre>";
print_r($dateMonthYearArr);
echo "</pre>";
?>

我得到了输出,

13/01/2011
21/01/2011
1325376000
1346454000
2012-01-01
2012-01-02
2012-01-03
2012-01-04
2012-01-05
2012-01-06
2012-01-07
2012-01-08
2012-01-09
2012-01-10
2012-01-11
2012-01-12
2012-01-13
2012-01-14
2012-01-15
2012-01-16
2012-01-17
2012-01-18
2012-01-19
2012-01-20
2012-01-21
2012-01-22
2012-01-23
2012-01-24
2012-01-25
2012-01-26
2012-01-27
2012-01-28
2012-01-29
2012-01-30
2012-01-31
2012-02-01
2012-02-02
2012-02-03
2012-02-04
2012-02-05
2012-02-06
2012-02-07
2012-02-08
2012-02-09
2012-02-10
2012-02-11
2012-02-12
2012-02-13
2012-02-14
2012-02-15
2012-02-16
2012-02-17
2012-02-18
2012-02-19
2012-02-20
2012-02-21
2012-02-22
2012-02-23
2012-02-24
2012-02-25
2012-02-26
2012-02-27

所以,你知道吗,它似乎对我有用。

虽然它没有产生我期望的数据,但是,它肯定没有表现出您所看到的行为。

Firstly I ran

<?
$from = "13/01/2011";
$to = "21/01/2011";

$fromDate = $from;
$toDate = $to;

echo $from."\n";
echo $to."\n";


$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);
echo $fromDateTS."\n";
echo $toDateTS."\n";

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."\n";
//print $currentDateStr..<br />.;
}

echo  "<pre>";
print_r($dateMonthYearArr);
echo "</pre>";
?>

I got output of

13/01/2011
21/01/2011
1325376000
1346454000
2012-01-01
2012-01-02
2012-01-03
2012-01-04
2012-01-05
2012-01-06
2012-01-07
2012-01-08
2012-01-09
2012-01-10
2012-01-11
2012-01-12
2012-01-13
2012-01-14
2012-01-15
2012-01-16
2012-01-17
2012-01-18
2012-01-19
2012-01-20
2012-01-21
2012-01-22
2012-01-23
2012-01-24
2012-01-25
2012-01-26
2012-01-27
2012-01-28
2012-01-29
2012-01-30
2012-01-31
2012-02-01
2012-02-02
2012-02-03
2012-02-04
2012-02-05
2012-02-06
2012-02-07
2012-02-08
2012-02-09
2012-02-10
2012-02-11
2012-02-12
2012-02-13
2012-02-14
2012-02-15
2012-02-16
2012-02-17
2012-02-18
2012-02-19
2012-02-20
2012-02-21
2012-02-22
2012-02-23
2012-02-24
2012-02-25
2012-02-26
2012-02-27

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.

埋情葬爱 2024-11-06 05:56:51
$fromDate = $from;
$toDate = $to;

date_default_timezone_set(date_default_timezone_get());
$dt = strtotime($fromDate);
$enddt = strtotime($toDate);
$dateMonthYearArr = array();
while ($dt <= $enddt) {
  $dateMonthYearArr[] = date("Y-m-d", $dt);
  $dt = strtotime('next day', $dt);
  }
$fromDate = $from;
$toDate = $to;

date_default_timezone_set(date_default_timezone_get());
$dt = strtotime($fromDate);
$enddt = strtotime($toDate);
$dateMonthYearArr = array();
while ($dt <= $enddt) {
  $dateMonthYearArr[] = date("Y-m-d", $dt);
  $dt = strtotime('next day', $dt);
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文