简体   繁体   English

将时间戳转换为.ics iCal兼容日期格式

[英]Convert a timestamp to an .ics iCal compatible date format

I have a date that I am storing as a timestamp through the formatDate function in jQuery. 我有一个日期,通过jQuery中的formatDate函数将其存储为时间戳。 I am then retriving this value to make an ics file, a calender file that adds the event time and details to the users calender. 然后,我将检索此值以创建一个ics文件,这是一个日历文件,它将事件时间和详细信息添加到用户日历中。 However the timestamp format isn't working in the ics file, the correct date isn't being added, so I need to convert it to a value that looks like 20091109T101015Z . 但是,时间戳格式在ics文件中不起作用,未添加正确的日期,因此我需要将其转换为类似于20091109T101015Z的值。 It's current format as a timestamp looks like 1344466800000 This is from this example which is what I am following to create my ics file. 它的当前格式为时间戳,看起来像1344466800000这是从此示例创建的,这是我遵循的创建ics文件的过程。

My link to the php file is http:// domain. 我到php文件的链接是http://域。 com/icsCreator.php?startDate=1344380400000&endDate=1345503600000&event=Space%20Weather%20Workshop&location=London com / icsCreator.php?startDate = 1344380400000&endDate = 1345503600000&event = Space%20Weather%20Workshop&location =伦敦

Current my ics file looks like 当前我的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";
...

?>

See if this works: 查看是否可行:

date('Ymd\THis', $time)

Here $time could be startDate or endDate from your query string. 在这里, $time可以是查询字符串中的startDateendDate If you don't want the time: 如果您不想要时间:

date('Ymd', $time)

NOTE (Thanks to Nicola) Here, $time must be a valid UNIX timestamp ie it must represent the number of seconds since the epoch. 注意 (感谢Nicola)在这里, $time必须是有效的UNIX时间戳,即,它必须表示自epoch以来的秒数。 If it represents the number of milliseconds, you need to first divide it by 1000. 如果它表示毫秒数,则需要先将其除以1000。

EDIT As pointed out by lars k , you need to add \\Z to the end of both the strings. 编辑正如lars k指出的那样,您需要在两个字符串的末尾添加\\Z

EDIT As pointed out by Nicola , you don't really need it. 编辑正如Nicola指出的那样,您实际上并不需要它。

The \\Z is telling the system the timezone. \\ Z告诉系统时区。 Z being Zulu or Universal time. Z是祖鲁语或世界时。

If you leave that out - then you are presuming that the timezone settings on the end users calendar application, match those of your system generating the timestamps. 如果您不进行此设置,那么您将假定最终用户日历应用程序上的时区设置与生成时间戳的系统的时区设置匹配。

In the USA alone there are multiple timezones - so you can't make that assumption just based on your users being in the same country as you. 仅在美国就有多个时区-因此您不能仅基于用户与您所在的国家/地区进行这种假设。

In order for dates to go into the calendar correctly, you need to specify the timezone offset from UTC as plus or minus Hours and Minutes 为了使日期正确进入日历,您需要将UTC的时区偏移量指定为正负小时和分钟。

NOTE: date('Ymd\\THisP') ; 注意:date('Ymd \\ THisP'); // P is an offset against GMT and should work for all calendar purposes. // P是GMT的偏移量,应适用于所有日历目的。

That would out put somthing like this for a 1hr shift from GMT 这样会使东西从格林尼治标准时间(GMT)移出1小时

20150601T10:38+01:00

When working with Dates in PHP it's best to use the DateTime object, then you can easily work with and change the timezones . 在PHP中使用Dates时,最好使用DateTime对象,然后可以轻松地使用它并更改时区

// 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') ;

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

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