【问题标题】:Fetch a range of dates using jQueryUI datepicker and php, in Spanish使用 jQueryUI datepicker 和 php 获取一系列日期,西班牙语
【发布时间】:2017-02-27 14:50:38
【问题描述】:

我正在尝试通过 2 个之前翻译成西班牙语的日期选择器来保存将由用户填写的日期范围。

问题是,当我在日期上使用 strtotime() 时,它将月份作为日期,反之亦然。

示例: 我选择了第 27 天 05 月和 2017 年,但返回值是不正确的日期格式,因为月份是 27。如果我选​​择 2017 年 01 月 05 日,那么它在数组中显示为 05 月 01 日和 2017 年。

这是我用来从输入文本中获取日期并生成日期范围的函数

function takedates() {
    if(isset($_POST['repS'])){      
        $dateStart = $_POST['txtdesde'];
        $dateEnd = $_POST['txthasta'];
        $fechaArray = generafechas($dateStart,$dateEnd);
    }

function generafechas($date1,$date2){
    $fecharray = array();
    if (is_string($date1) === true){            
        $deit1 = strftime("%d-%m-%Y",strtotime($date1));            
    }
    if (is_string($date2) === true){            
        $date2 = strftime("%d-%m-%Y",strtotime($date2));            
    }
    do {
        $fecharray[] = date("m-d-Y", $date1);
         $date1 = strtotime("+1 day", $date1);  
    } while($date1 <= $date2);
    return $fecharray;
}
?>

我的问题是:如何用西班牙日期格式的日期填充数组?

PS:我已经在使用这些函数的文件中使用了setLocale(LC_TIME,'es_ES'),并且输入显示的日期类似于“dd/mm/yyyy”

【问题讨论】:

  • 请用英文写问题
  • 我相信这个网站有一个西班牙语版本,您可以在其中发布这个问题。如果您希望将其保留在此处,则需要将其翻译成英文。
  • 欢迎来到 Stack Overflow!抱歉,本网站要求您仅用 英语 发表您的问题。请自行翻译;其他人为您翻译不会帮助您理解 cmets 和答案,也不会回复反馈。
  • Yo creo tú publicas en la versión en español de este sitio web。 (以防他/她看不懂英文)
  • 哦,好的,我可以翻译,我不知道有西班牙文版本,还以为是同一个网站,谢谢

标签: java php jquery datepicker strftime


【解决方案1】:

strtotime 在解析日期时间字符串时不会考虑您的语言环境。如果您使用由斜线分隔的日期,则假定为美式m/d/y。如果您使用由句点分隔的日期(如果年份是四位数,则使用破折号),则假定它是世界其他风格的(d.m.yd-m-Y)。 (请注意,如果您只使用两位数年份并使用破折号,PHP 将尝试将其解析为 y-m-d。)

您应该使用date-create-from-format / DateTime::createFromFormat 来获取DateTime 对象,而不是strtotime,然后从中构建您的日期字符串。

基于评论的更新:为了获得您想要的输出,您需要使用intl 扩展的IntlDateFormatter class 来进行输出。

修改上面的代码(未经测试):

function generafechas($date1,$date2){
    $fecharray = array();

    if (is_string($date1) && is_string($date2)){
        // These lines assume the input is formatted `day-month-year`,
        // with 2-digit day and month, and 4-digit year.
        $date1 = DateTime::createFromFormat('d-m-Y', $date1)
        $date2 = DateTime::createFromFormat('d-m-Y', $date2)
    } else {
        throw new InvalidArgumentException('Must provide two date strings');
    }

    // Create the formatter
    $formatter = IntlDateFormatter::create('es_ES', null, null, null, null, "d 'de' MMMM 'del' yyyy");

    do {
        // This line prints the first date in the format you chose above
        $fecharray[] = $formatter->format($date1);
        $date1->add(new DateInterval("P1D")); // `P1D` means "Duration of 1 Day" in the ISO 8601 standard
    } while($date1 <= $date2);

    return $fecharray;
}

如果您随数据一起提供区域设置,您可以根据需要更改createFromFormat 中使用的格式字符串。

【讨论】:

  • 非常感谢您的回答,但我正在使用现有数据处理数据库,我无法更改它,这就是为什么我必须创建一个数组来选择一个范围日期,因为数据库中的日期以 Varchar 形式插入,并且以这种格式示例:“18 de mayo del 2016”所以很痛苦,无论如何感谢您提供的信息
  • @Sojansons 您需要做的就是更改输出格式。我已经更新了我的答案。
  • 哦,天哪,非常感谢这确实有效,我找到了一种解决方法,但这节省了我的行数,而且效果更好!再次感谢。
猜你喜欢
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 2022-11-20
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多