有没有办法用 Postgres 存储阿拉伯日期?
我是一名 PHP 开发人员,正在开发一个项目,需要在数据库中存储阿拉伯日期。
不过,我已经创建了该应用程序并在其中嵌入了 JavaScript 阿拉伯日历。
现在,当我选择一个日期(例如 12/02/1442
)并按下“保存”按钮时,会出现以下错误:
Year Out Of Range
I am a PHP developer and I am working on a project in which I need to store Arabic dates in my database.
However, I have created the application and embedded a JavaScript Arabic calender in it.
Now when I select a date, e.g 12/02/1442
, and press the save button, it gives me the following error:
Year Out Of Range
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些日期(阿拉伯语、Jalali 等)通常会出现 SQL 服务器问题。根据我的经验,有 3 种方法可以处理它:
使用字符类型将日期存储到具有任何自定义格式的字段中。 (问题是:操作、算术、排序……它们很难实现)
使用整数或固定字符类型来存储日期,例如<代码>14420101代表
1/1/1442
。 (与第一个相同,但解决了排序问题)使用date或timestamp类型存储,应将日期转换为标准日期格式后再存储到数据库中,当你阅读它们时将它们转换回来。该方法需要外部编程语言来处理(通常有外部语言)。或者您可以在数据库中编写内部函数。
只需存储日期然后方法 1 和 2 就足够了。
否则,我建议使用第三种方法,因为您可以使用数据库服务器或编程语言中存在的
date
和time
预定义函数。例如,您可以在 PHP 中轻松使用
date_diff()
,或者使用 postgres 内部函数来处理日期。These dates (Arabic, Jalali, ...) usually have problems with SQL servers. In my experience, there are 3 ways to handle it:
Using character types for storing dates into the fields with any custom format. (The problem are: Manipulating, arithmetic, sorting,... which they are hard to implement)
Using integer or fixed character type for storing dates, like
14420101
for1/1/1442
. (Same as first one, but sorting is solved)Using date or timestamp type for storing, you should convert dates to standard date format before storing it into the database, and when you're reading them convert them back. This method needs a external programming language to handle (Usually there is an external language). Or you can write a internal functions in your database.
Just storing dates then methods 1 and 2 are enough.
Otherwise, I suggest third approach, because you can use
date
andtime
pre-defined functions which exists in database servers or programming languages.For example you can use
date_diff()
easily in PHP, or use postgres internal functions for dates.我认为这将是编写自定义数据类型的完美应用程序。也许有人已经做到了,或者你可以开始。
I think this would be a perfect application to write a custom data type. Maybe someone has already done it, or you could start.
PostgreSQL 支持以 Julian Day 形式输入和输出日期和时间戳。手册中的两个相应部分:
如果您想在数据库中进行计算,这可能会有所帮助。如果这不是问题,只需按照 Masoud M. 的建议存储日期即可。
PostgreSQL supports input and output of dates and timestamps as Julian Day. The two appropriate sections in the manual:
That might help if you want to do calculations in the database. If that's not an issue, just store the dates as Masoud M. has suggested.