开发者

Reconstructing a timestamp with $_GET values

开发者 https://www.devze.com 2023-04-02 11:06 出处:网络
I\'m trying to deconstructing current timestamp and then use mktime(...) to reconstruct it using values passed through $_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 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"));

?>

Outputs:

September 3, 2011

February 3, 2011

http://codepad.org/NWLt7ER6

EDIT

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");
}

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

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"));

?>

Outputs:

October 1, 2011

March 3, 2011

http://codepad.org/RFXTze5z


Ok based on your given specifications:

$new_day = isset($_GET['day']) ? $_GET['day'] : date("d");
$new_month = isset($_GET['month']) ? $_GET['month'] : false;
$new_year = isset($_GET['year']) ? $_GET['year'] : false;

if ($new_month and $new_year) {
    $date = strtotime("$new_month/$new_day/$new_year");
}

And I gave you something extra.. Maybe comes in handy^^

0

精彩评论

暂无评论...
验证码 换一张
取 消