0

I have php code that pulls information needed from the database. The php date code is formatted in "format("Y-m-d H:i:s")". I am trying to get these dates from the database and add them to a calendar using JavaScript. I loop there the events that I want to add to the calendar. I have to different ways I found solutions for but neither one works.

<script type='text/javascript'>
    <?php for($x = 0; $x < count($events); $x++) { ?>
          var dateToConvert = new Date('<?php echo date(DATE_ISO8601, strtotime($events[$x]["startDate"])); ?>');
          var startDate = dateToConvert.toISOString();
          var endDate = '<?php echo date(DATE_ISO8601, strtotime($events[$x]["endDate"])); ?>';
          var events = [
             {
               title: '<?php echo $events[$x]["name"]; ?>',
               start: new Date(startDate),
               end: endDate
             }
          ];
    <?php } ?>
</script>

I am assuming that I am missing something minor but I can't figure it out. I used the Chrome debugger and this is what I have as input. (2013-03-18T01:00:00-0600) It doesn't seem to have the Z at the end like it should.

The value in the database is (2013-03-18 01:00:00).

What am I missing?

4

2 回答 2

0

您的 ISO 时间格式正确。日期时间末尾的“Z”表示“祖鲁时间”,即 UCT 时区。

因此,如果您没有指定不同的时区,您只会指定“Z”。

在您的情况下,您引用的日期确实包含时区 ( -6000),所以我不希望看到“Z”。

有关更多信息,请参见维基百科页面:http ://en.wikipedia.org/wiki/ISO_8601#Time_zone_designators

对于在 PHP 和 Javascript 之间移动日期/时间值,我想说最简单的方法就是使用 unix 时间戳。这是一个简单的整数值,因此无需考虑格式,两种语言都可以完美理解它。唯一需要注意的是,在 Javascript 中它的单位是微秒,而 PHP 是秒,因此您需要乘以或除以 1000。但这很简单。

于 2013-03-14T22:07:33.000 回答
0

您不应以任何字符串格式传递日期。相反,将它们作为数字传递。

var dateToConvert = new Date();
dateToConvert.setTime(<?php echo strtotime($events[$x]['startDate']); ?>000);

这将更可靠地工作。请注意,存在三个零是因为 JS 以毫秒为单位处理时间,而 PHP 以秒为单位处理时间。

于 2013-03-14T20:34:18.377 回答