Esempio n. 1
0
/**
 * Decodes a RFC 822 Date-header into a timestamp
 *
 * @param array dateParts the Date-header split by whitespace
 * @return int the timestamp calculated from the header
 */
function getTimeStamp($dateParts)
{
    /** $dateParts[0] == <day of week>   Mon, Tue, Wed
     ** $dateParts[1] == <day of month>  23
     ** $dateParts[2] == <month>         Jan, Feb, Mar
     ** $dateParts[3] == <year>          1999
     ** $dateParts[4] == <time>          18:54:23 (HH:MM:SS)
     ** $dateParts[5] == <from GMT>      +0100
     ** $dateParts[6] == <zone>          (EDT)
     **
     ** NOTE:  In RFC 822, it states that <day of week> is optional.
     **        In that case, dateParts[0] would be the <day of month>
     **        and everything would be bumped up one.
     **/
    /*
     * Simply check to see if the first element in the dateParts
     * array is an integer or not.
     * Since the day of week is optional, this check is needed.
     */
    if (count($dateParts) < 2) {
        return -1;
    }
    /* remove day of week */
    if (!is_numeric(trim($dateParts[0]))) {
        $dataParts = array_shift($dateParts);
    }
    /* calculate timestamp separated from the zone and obs-zone */
    $stamp = strtotime(implode(' ', array_splice($dateParts, 0, 4)));
    if (!isset($dateParts[0])) {
        $dateParts[0] = '+0000';
    }
    if (!preg_match('/^[+-]{1}[0-9]{4}$/', $dateParts[0])) {
        /* zone in obs-zone format */
        if (preg_match('/\\((.+)\\)/', $dateParts[0], $regs)) {
            $obs_zone = $regs[1];
        } else {
            $obs_zone = $dateParts[0];
        }
        return getGMTSeconds($stamp, $obs_zone);
    } else {
        return getGMTSeconds($stamp, $dateParts[0]);
    }
}
Esempio n. 2
0
/**
 * Decodes a RFC 822 Date-header into a timestamp
 *
 * @param array dateParts the Date-header split by whitespace
 * @return int the timestamp calculated from the header
 */
function getTimeStamp($dateParts)
{
    /* $dateParts[0] == <day of week>   Mon, Tue, Wed
     * $dateParts[1] == <day of month>  23
     * $dateParts[2] == <month>         Jan, Feb, Mar
     * $dateParts[3] == <year>          1999
     * $dateParts[4] == <time>          18:54:23 (HH:MM:SS)
     * $dateParts[5] == <from GMT>      +0100
     * $dateParts[6] == <zone>          (EDT)
     *
     * NOTE:  In RFC 822, it states that <day of week> is optional.
     *        In that case, dateParts[0] would be the <day of month>
     *        and everything would be bumped up one.
     */
    if (count($dateParts) < 2) {
        return -1;
    } else {
        if (count($dateParts) == 3) {
            if (substr_count($dateParts[0], '-') == 2 && substr_count($dateParts[1], ':') == 2) {
                //  dd-Month-yyyy 23:19:05 +0200
                //  redefine the date
                $aDate = explode('-', $dateParts[0]);
                $newDate = array($aDate[0], $aDate[1], $aDate[2], $dateParts[1], $dateParts[2]);
                $dateParts = $newDate;
            }
        }
    }
    /*
     * Simply check to see if the first element in the dateParts
     * array is an integer or not.
     * Since the day of week is optional, this check is needed.
     */
    if (!is_numeric(trim($dateParts[0]))) {
        /* cope with broken mailers that send "Tue,23" without space */
        if (preg_match('/^\\w+,(\\d{1,2})$/', $dateParts[0], $match)) {
            /* replace Tue,23 with 23 */
            $dateParts[0] = $match[1];
        } else {
            /* just drop the day of week */
            array_shift($dateParts);
        }
    }
    /* calculate timestamp separated from the zone and obs-zone */
    $stamp = strtotime(implode(' ', array_splice($dateParts, 0, 4)));
    if (!isset($dateParts[0])) {
        $dateParts[0] = '+0000';
    }
    if (!preg_match('/^[+-]{1}[0-9]{4}$/', $dateParts[0])) {
        /* zone in obs-zone format */
        if (preg_match('/\\((.+)\\)/', $dateParts[0], $regs)) {
            $obs_zone = $regs[1];
        } else {
            $obs_zone = $dateParts[0];
        }
        return getGMTSeconds($stamp, $obs_zone);
    } else {
        return getGMTSeconds($stamp, $dateParts[0]);
    }
}