简体   繁体   English

CodeIgniter/JQuery/MySQL 日期时间

[英]CodeIgniter/JQuery/MySQL DateTime

Within my CodeIgniter app, I'm using a Jquery calendar pop-up that also captures time as set by the user, so the end result looks like: MM-DD-YYYY HH:MM, and I'm storing this in MySQL into a DateTime field that is: YYYY-MM-DD HH:MM:SS.在我的 CodeIgniter 应用程序中,我使用 Jquery 日历弹出窗口,它还捕获用户设置的时间,因此最终结果看起来像:MM-DD-YYYY HH:MM,我将其存储在 Z62A0404B959467BB3 中的 Z62A0404B959467BB3 中一个日期时间字段,即:YYYY-MM-DD HH:MM:SS。 What is the best (most efficient) way to push the date/time into MySQL so that it saves properly, and to pull is back out of MySQL and render it on the screen in the reverse format?将日期/时间推入 MySQL 以便正确保存,并将其拉出 MySQL 并以相反格式在屏幕上呈现的最佳(最有效)方法是什么? Thanks!谢谢!

Most efficient way is to use the ISO 8601 standard to pass date values between the client and server.最有效的方法是使用ISO 8601标准在客户端和服务器之间传递日期值。 Since the client and server talks in strings you'd be parsing the date to a string before sending it either way.由于客户端和服务器以字符串形式进行对话,因此您需要先将日期解析为字符串,然后再以任何一种方式发送。 The best format I prefer is the combined date and time in UTC :我更喜欢的最佳格式是UTC 中的组合日期和时间

2011-06-14T13:57Z 2011-06-14T13:57Z

There are no spaces and it's clean.没有空间而且很干净。 Then you'll have to parse it on the server side (should be relatively easy using PHP) and parse it on the client side.然后你必须在服务器端解析它(使用 PHP 应该相对容易)并在客户端解析它。

For displaying purposes, I prefer to extend JavaScript's Date.prototype to include a format function that imitates PHP's date format .为了显示的目的,我更喜欢扩展 JavaScript 的 Date.prototype以包含一个格式 function ,它模仿PHP 的日期格式

Once you include the linked script from above you could do this on the server side -包含上面的链接脚本后,您可以在服务器端执行此操作 -

var today = new Date();
alert(today.format('m-d-Y H:i')); //displays "06-14-2011 11:18"

Good luck!祝你好运!

I think you should use the strptime() function to parse the date received from the jQuery calendar your using and using mktime() :我认为您应该使用strptime() function 来解析从 jQuery 日历收到的日期,您使用和使用mktime()

// Parse the time based on your jQuery calendar's format
$parts = strptime($calendar_value, '%m-%d-%Y %H:%M');

if ( ! empty($parts) )
{
  // Create a Unix timestamp
  $timestamp = mktime($parts['tm_hour'], $parts['tm_min'], 0, $parts['tm_mon'] + 1, $parts['tm_mday'], $parts['tm_year'] + 1900);

  // Create a string representation of the Unix timestamp
  $date = date(DATE_ISO8601, $timestamp);
}

You'll want to use $date to insert in your database.您需要使用$date插入数据库。 There is a function called "strtotime" which will attempts to parse dates that are in human-readable format but I doubt it's able to determine if the month or day comes first, especially if they're both lower than 12 which is why I chose to use "strptime" instead.有一个名为“strtotime”的 function 将尝试解析人类可读格式的日期,但我怀疑它是否能够确定月份或日期是否在前,特别是如果它们都低于 12,这就是我选择的原因改为使用“strptime”。

When you pull the data from MySQL, you can then simply use the date() and strtotime() function to populate the calendar:当您从 MySQL 提取数据时,您可以简单地使用date()strtotime() function 来填充日历:

echo date('m-d-Y h:i', strtotime($mysql_date));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM