简体   繁体   中英

Covert html date value to RFC 2822

I have following code,

$date = $_POST["s-date"]; // get from  form. value pass correctly. 
 $time =  "".$hour.":".$minute.":".$second.""; // ok
   echo RFC2822($date,$time);   

function RFC2822($date,$time = '00:00') { 
   $array =  list($d, $m, $y) = explode('-', $date); 
    list($h, $i, $s) = explode(':', $time);

    return date('r', mktime($h,$i,0,$m,$d,$y));
}

When I execute this script, time fragment is printed correctly, But date is displayed as Jul 08 2025 always even passing different date to the function. How to fix this.

You must need to check the value of this input $_POST["s-date"] it must be follow this format of date:

`DD-MM-YY`

because, you are creating list in this format (DD-MM-YY):

$array =  list($d, $m, $y) = explode('-', $date);

UPDATE 1:

As you mentioned, you are using HTML date and HTML date format is 'YYYY-MM-DD' , now just change the list variable order as per date format.

From what I can gather your problem lies not with the code you've shared, but somewhere else. If I copy-paste your script and put in a fixed date on $date, it works as expected. (I've removed $time for missing vars)

$date = "27-02-2014";
echo RFC2822($date);   

function RFC2822($date,$time = '00:00:00') {
   $array =  list($d, $m, $y) = explode('-', $date);
    list($h, $i, $s) = explode(':', $time);

    return date('r', mktime($h,$i,0,$m,$d,$y));
}

Outputs

Thu, 27 Feb 2014 12:00:00 +0100

From this I expect that your $_POST['s-date'] var somehow gets a fixed 08-07-2025 or some other input that will make it 08 Jul 2025.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM