用$ _GET值重建时间戳 [英] Reconstructing a timestamp with $_GET values
问题描述
我正在尝试解构当前时间戳,然后使用mktime(...)使用通过$ _GET
I'm trying to deconstructing current timestamp and then use mktime(...) to reconstruct it using values passed through $_GET
的值重构它这是我的代码到目前为止。
Here is my code so far.
$date =time ();
if(!empty($_GET['month'])){
if(!empty($_GET['year'])){
$f = getdate($date);
$date = mktime($f["hours"], $f["minutes"], $f["seconds"], $_GET['month'],
$f["days"], $_GET['year']);
}
}
$ date稍后使用,它仍然等于当前time()。
$date is used later on and it still equals current time().
推荐答案
<?php
$month = 2;
$year = 11;
echo date('F j, Y', strtotime("now"))."\n";
echo date('F j, Y', strtotime("$month/".date('d')."/$year"));
?>
输出:
2011年9月3日
September 3, 2011
2011年2月3日
< a href =http://codepad.org/NWLt7ER6 =nofollow> http://codepad.org/NWLt7ER6
编辑
此外,就检查输入而言,我将其设置为仅接受数值,并验证这些值。
Also, as far as checking the input, I would set it up to only accept numeric values, and validate those.
$get_month = (int)$_GET['month'];
$get_year = (int)$_GET['year']; // This should be a 4 digit year; no '00' - '09' to deal with
// The year check is up to you what range you accept
if (($get_month > 0 && $get_month <= 12) && ($get_year > 1900 && $get_year < 2100)) {
$get_date = strtotime("$get_month/".date('d')."/$get_year");
}
您也可能希望将其放在函数中并调用它,使用它或者使用比 $ date
更具体的全局变量名。
You also might want to put that in a function and call it, use it in an object scope, or use more specific global variable names than $date
.
编辑
EDIT
如同利润指出的那样,使用当天不再存在一天的日子不会进入下个月(9月和2月没有31天):
And as profitphp points out, using a day for another month when that day doesn't exists pushes into the next month (September and February do not have 31 days):
<?php
$month = 2;
$day = 31;
$year = 11;
echo date('F j, Y', strtotime(date('m')."/$day/".date('Y')))."\n";
echo date('F j, Y', strtotime("$month/$day/$year"));
?>
输出:
2011年10月1日
October 1, 2011
2011年3月3日
< a href =http://codepad.org/RFXTze5z =nofollow> http://codepad.org/RFXTze5z
这篇关于用$ _GET值重建时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!