0

可能重复:
一直到 UTF-8

我正在尝试将当前日期转换为当前的犹太日期。为了做到这一点,我找到了这个php 函数。但是,当我将代码打印到屏幕上时,我只会在屏幕上看到奇怪的字符。经过更多调查后,我发现这只发生在我遍历一组日期时。

为了显示:

代码示例 1

我的代码:

$aJulianFormat = array('month'=>date('m'), 'day'=>date('d'), 'year' => date('Y'));
$jewishDate = jdtojewish(
               gregoriantojd($aJulianFormat['month'], 
                             $aJulianFormat['day'],
                             $aJulianFormat['year']),
                             true,
                             CAL_JEWISH_ADD_GERESHAYIM + 
                             CAL_JEWISH_ADD_ALAFIM + 
                             CAL_JEWISH_ADD_ALAFIM_GERESH
              );
echo('jdtojewish' . ' : ' . $jewishDate . '<br>');
echo('iconv' . ' : ' . iconv('ISO-8859-8', 'UTF-8', $jewishDate) . '<br>');

输出:

jdtojewish : �' ���� �' ��������"�</p>

iconv : ד' תמוז ה' אלפים תשע"ב <----- 这是所需的输出,很好。

代码示例 2

    $aDates = $this->createDateRangeArray('2012-09-01', '2013-09-01');
    foreach ($aDates as $aDate) {

        $aJulianFormat = $this->splitDate($aDate);
        //         $aJulianFormat = array('month'=>date('m'), 'day'=>date('d'), 'year' => date('Y'));
        $jewishDate = jdtojewish(
                gregoriantojd($aJulianFormat['month'], 
                        $aJulianFormat['day'], 
                        $aJulianFormat['year']), 
                        true, 
                        CAL_JEWISH_ADD_GERESHAYIM +
                        CAL_JEWISH_ADD_ALAFIM +
                        CAL_JEWISH_ADD_ALAFIM_GERESH
        );
        echo('jdtojewish' . ' : ' . $jewishDate . '<br>');
        echo('iconv' . ' : ' . iconv('ISO-8859-8', 'UTF-8', $jewishDate) . '<br>');

    }

输出

jdtojewish : é"ã àìåì ä' àìôéí úùò"á iconv : ×™"ד ×לול ×”'

××œ×¤×™× ×ª×©×¢"×' jdtojewish : è"å àìåì ä' àìôéí úùò"á iconv :

×∼"ו ×לול ×"' ××œ×¤×™× ×ª×©×¢"×' jdtojewish : è"æ àìåì ä' àìôéí

úùò"á iconv : ×∼"×– ×לול ×"' ××œ×¤×™× ×ª×©×¢"×' jdtojewish : é"æ

àìåì ä' àìôéí úùò"á iconv : ×™"×– ×לול ×"' ××œ×¤×™× ×ª×©×¢"×'</p>

jdtojewish : é"ç àìåì ä' àìôéí úùò"á iconv : ×™"×— ×לול ×”'

××œ×¤×™× ×ª×©×¢"×' jdtojewish : é"è àìåì ä' àìôéí úùò"á iconv :

×™"×~ ×לול ×”' ××œ×¤×™× ×ª×©×¢"×' jdtojewish : ë' àìåì ä' àìôéí

úùò"á iconv : ×›' ×לול ×"' ××œ×¤×™× ×ª×©×¢"×' jdtojewish : ë"à

àìåì ä' àìôéí úùò"á iconv : ×›"× ×לול ×”' ××œ×¤×™× ×ª×©×¢"×'</p>

jdtojewish : ë"á àìåì ä' àìôéí úùò"á iconv : ×›"×' ×לול ×”'

××œ×¤×™× ×ª×©×¢"×' jdtojewish : ë"â àìåì ä' àìôéí úùò"á

ETC....

这都是错误的/不是所需的输出。

编码似乎出了点问题。

对于那些有兴趣的人,我在这里发布了我的完整代码:http ://pastebin.com/kc2LHPrg :

<?php

namespace Luach\Controller;

use Zend\Mvc\Controller\ActionController,
    Zend\View\Model\ViewModel;

class LuachController extends ActionController {

    public function indexAction() {
        $aDates = $this->createDateRangeArray('2012-09-01', '2013-09-01');
        foreach ($aDates as $aDate) {

            $aJulianFormat = $this->splitDate($aDate);
            //         $aJulianFormat = array('month'=>date('m'), 'day'=>date('d'), 'year' => date('Y'));
            $jewishDate = jdtojewish(
                    gregoriantojd($aJulianFormat['month'], 
                            $aJulianFormat['day'], 
                            $aJulianFormat['year']), 
                            true, 
                            CAL_JEWISH_ADD_GERESHAYIM +
                            CAL_JEWISH_ADD_ALAFIM +
                            CAL_JEWISH_ADD_ALAFIM_GERESH
            );
            echo('jdtojewish' . ' : ' . $jewishDate . '<br>');
            echo('iconv' . ' : ' . iconv('ISO-8859-8', 'UTF-8', $jewishDate) . '<br>');

        }


    }

    private function createDateRangeArray($strDateFrom, $strDateTo) {
        // takes two dates formatted as YYYY-MM-DD and creates an
        // inclusive array of the dates between the from and to dates.
        // could test validity of dates here but I'm already doing
        // that in the main script

        $aryRange = array();

        $iDateFrom = mktime(1, 0, 0, substr($strDateFrom, 5, 2), substr($strDateFrom, 8, 2), substr($strDateFrom, 0, 4));
        $iDateTo = mktime(1, 0, 0, substr($strDateTo, 5, 2), substr($strDateTo, 8, 2), substr($strDateTo, 0, 4));

        if ($iDateTo >= $iDateFrom) {
            array_push($aryRange, date('Y-m-d', $iDateFrom)); // first entry

            while ($iDateFrom < $iDateTo) {
                $iDateFrom+=86400; // add 24 hours
                array_push($aryRange, date('Y-m-d', $iDateFrom));
            }
        }
        return $aryRange;
    }

    private function splitDate($sDate) {

        return date_parse_from_format("Y-m-d", $sDate);

    }

}

有人知道第二个代码示例出了什么问题吗?

非常感谢。

4

0 回答 0