2

我有一个日期,我通过jQuery 中的formatDate函数将其存储为时间戳。然后,我将检索此值以制作一个 ics 文件,这是一个日历文件,它将事件时间和详细信息添加到用户日历中。但是时间戳格式在 ics 文件中不起作用,没有添加正确的日期,因此我需要将其转换为看起来像20091109T101015Z. 它的当前格式为时间戳看起来像1344466800000这是来自这个示例,这是我创建我的 ics 文件所遵循的。

我的 php 文件链接是 http:// 域。com/icsCreator.php?startDate=1344380400000&endDate=1345503600000&event=Space%20Weather%20Workshop&location=伦敦

当前我的 ics 文件看起来像

<?php
$dtStart=$_GET['startDate'];
$dtEnd=$_GET['endDate'];
$eventName=$_GET['event'];
$location=$_GET['location'];

...
echo "CREATED:20091109T101015Z\n";
echo "DESCRIPTION:$eventName\n";
echo "DTEND:$dtEnd\n";    
echo "DTSTART:".$dtStart."\n";
echo "LAST-MODIFIED:20091109T101015Z\n";
echo "LOCATION:$location\n";
...

?>
4

2 回答 2

6

看看这是否有效:

date('Ymd\THis', $time)

这里$time可能是startDateendDate来自您的查询字符串。如果你不想要时间:

date('Ymd', $time)

注意(感谢 Nicola)这里,$time必须是一个有效的 UNIX 时间戳,即它必须表示自纪元以来的秒数。如果代表毫秒数,需要先除以1000。

编辑正如lars k所指出的,您需要添加\Z到两个字符串的末尾。

编辑正如Nicola所指出的,你并不真的需要它。

于 2012-08-03T10:32:55.900 回答
5

\Z 告诉系统时区。Z 是祖鲁语或世界时。

如果您将其排除在外 - 那么您假设最终用户日历应用程序上的时区设置与生成时间戳的系统相匹配。

仅在美国就有多个时区 - 因此您不能仅根据您的用户与您在同一个国家/地区做出该假设。

为了使日期正确进入日历,您需要指定与 UTC 的时区偏移量为正负小时和分钟

注意:日期('Ymd\THisP');// P 是相对于 GMT 的偏移量,应该适用于所有日历用途。

从格林威治标准时间转变 1 小时后,就会出现这样的情况

20150601T10:38+01:00

在 PHP 中使用 Dates 时,最好使用DateTime对象,这样您就可以轻松地使用和更改timezones

// Start with your local timezone e.g
$timezone = new \DateTimeZone('Europe/Amsterdam') ; 

// Don't be tempted to use a timezone abbreviation like EST
// That could mean Eastern Standard Time for USA or Australia.
// Use a full timezone: http://php.net/manual/en/timezones.php

$eventdate = new DateTime ( '1st September 2015 10:30', $timezone);

// Convert the time to Universal Time
$eventdate->setTimezone( new DateTimeZone('UTC') ) ; // Universal / Zulu time

// Return Event Date/Time in calendar ICS friendly format 
// comfortable in the knowledge that it is really in UTC time
return $eventdate->format('Ymd\THis\Z') ;
于 2015-06-01T13:22:24.987 回答