将PHP日期格式转换为jQueryUI Datepicker日期格式 [英] Convert a PHP date format to a jQueryUI Datepicker date format
问题描述
:我想人们有问题要明白我的意思,所以我完全重写了我的解释。
我的工作在用户可以定义整个站点中使用的日期格式的项目上。它使用PHP日期格式标准。例如:年 - 月 - 日由Ymd设置。
I work on a project where users can define a date format used in the whole site. It uses PHP date format standard. For example : "year-month-day" is set by "Y-m-d".
PHP标准使用单字符符号,如 Y,m,d,F ,j 来描述日期格式。如文档中所示: http://www.php.net/manual/fr /function.date.php
PHP standard uses single-character symbols like Y, m, d, F, j to describe the date format. As seen in the documentation : http://www.php.net/manual/fr/function.date.php
有时用户可以通过jQueryUI Datepicker选择日期。该组件使用代码字描述其日期格式,如 yy,y,mm,dd,D,...
http://api.jqueryui.com/datepicker/#utility-formatDate
Sometimes users can select a date thanks to a jQueryUI Datepicker. This component describes its date format with code-words like yy, y, mm, dd, D, ... http://api.jqueryui.com/datepicker/#utility-formatDate
我会喜欢以相同的格式显示PHP和Datepicker的日期。
我的意思是PHP应该以用户设置的格式输出日期,并且Datepicker应该以相同的格式显示所选日期。
I would like to display the dates in the same format for both PHP and the Datepicker. I mean that PHP should output the date as in the format set by user, AND the Datepicker should show the selected date in the same format.
鉴于:
- 日期格式必须描述为PHP风格
- 我不知道先验用户设置的格式
- The date format is necessarily described "PHP style"
- I can't know a priori which format was set by users
/!\这不是如何从已知格式读取/解析/显示日期的问题。
/!\ This not a problem of how to read/parse/display a date from a known format.
不幸的是,Javascript日期格式描述不同于用PHP。
例如,这两种日期格式是等价的,但在PHP和Javascript中有不同的描述:
Unfortunately, Javascript date format description is not the same as in PHP. For instance, these 2 date formats are equivalent but described differently in PHP and Javascript:
- PHP:Ymd(由用户设置) )
- Javascript:yy-mm-dd
正如你所看到的,我不能只是使用PHP日期格式配置datepicker,因为它会被误解或根本不被识别。
As you can see, I cannot just configure the datepicker with the PHP date format, because it will be misunderstood, or not recognized at all.
有人(在下面的答案中)建议创建我自己的日期格式标准转换器,将每个PHP符号与JS日期格式描述中的等效符号相匹配。就像:
Someone (in answers below) adviced to create my own "date format standard converter", matching each PHP symbol with its equivalent in JS date format description. Just like:
- Y => yy
- m => mm
- d => dd
- y => y
- z => o
- ...
- Y => yy
- m => mm
- d => dd
- y => y
- z => o
- ...
然后将每个PHP符号替换为JS符号。因此,d / m / Y将被神奇地翻译成dd / mm / yy。
And then replace each PHP symbol with the JS one. And so "d/m/Y" will be translated into "dd/mm/yy", magically.
但也许有人知道让jQueryUI Datepicker理解的另一种正确方法PHP日期格式标准?
But maybe somebody knows another proper way to make jQueryUI Datepicker understand PHP date format standard?
编辑:我写了一个解释问题和解决方案的教程。进一步阅读: http:/ /tristan-jahier.fr/blog/2013/08/convertir-un-format-de-date-php-en-format-de-date-jqueryui-datepicker
推荐答案
我选择了残酷的方法:将符号逐个符号转换为日期格式。
我制作了一个'not-so-dummy'代码片段。
I chose the brutal method : converting symbol-by-symbol the date format. I made a 'not-so-dummy' code snippet.
/*
* Matches each symbol of PHP date format standard
* with jQuery equivalent codeword
* @author Tristan Jahier
*/
function dateformat_PHP_to_jQueryUI($php_format)
{
$SYMBOLS_MATCHING = array(
// Day
'd' => 'dd',
'D' => 'D',
'j' => 'd',
'l' => 'DD',
'N' => '',
'S' => '',
'w' => '',
'z' => 'o',
// Week
'W' => '',
// Month
'F' => 'MM',
'm' => 'mm',
'M' => 'M',
'n' => 'm',
't' => '',
// Year
'L' => '',
'o' => '',
'Y' => 'yy',
'y' => 'y',
// Time
'a' => '',
'A' => '',
'B' => '',
'g' => '',
'G' => '',
'h' => '',
'H' => '',
'i' => '',
's' => '',
'u' => ''
);
$jqueryui_format = "";
$escaping = false;
for($i = 0; $i < strlen($php_format); $i++)
{
$char = $php_format[$i];
if($char === '\\') // PHP date format escaping character
{
$i++;
if($escaping) $jqueryui_format .= $php_format[$i];
else $jqueryui_format .= '\'' . $php_format[$i];
$escaping = true;
}
else
{
if($escaping) { $jqueryui_format .= "'"; $escaping = false; }
if(isset($SYMBOLS_MATCHING[$char]))
$jqueryui_format .= $SYMBOLS_MATCHING[$char];
else
$jqueryui_format .= $char;
}
}
return $jqueryui_format;
}
此函数处理PHP和Datepicker日期格式标准之间的所有常用代码字。
This function handles all the common codewords between PHP and Datepicker date format standards.
另外,我添加了对字符转义的支持:
Plus, I added support for character escaping :
dm \\\ f Y
变为 dd mm'的'yy
您可能仍然遇到问题像'W','L'这样的符号没有由Datepicker处理的等价物。
You may still have problems with symbols like 'W', 'L' that have no equivalent handled by Datepicker.
这篇关于将PHP日期格式转换为jQueryUI Datepicker日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!