php:写一个.ics(iCal)文件?日期格式化?

时间:2012-09-02 09:46:26

标签: php date calendar icalendar

我不是一个PHP专家,并且无法格式化.ics文件的日期。

所以我有一个循环为每个$post生成一个日历条目(在我的情况下,$ post是一个事件)

foreach( $posts as $post ) : setup_postdata($post);
        $ical .= "BEGIN:VEVENT
        UID:" . md5(uniqid(mt_rand(), true)) . "mysite.com
        DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
        DTSTART:".get_event_date($post)."00Z
        DTEND:".get_event_end_date($post)."00Z
        SUMMARY:".get_the_title($post->ID)."
        DESCRIPTION:".get_the_excerpt($post->ID)."
        END:VEVENT";
    endforeach;

我只是格式化日期的问题,这就是为什么我完全摆脱它并希望你们可以帮助我。

我有两个函数get_event_date($post)get_event_end_date($post),它们返回时间戳1258665163。如何将此时间戳转换为iCal .ics文件的正确格式?

此外,我还想添加每个事件的time。 这两个函数称为get_event_time($post)get_event_end_time($post),它们都按以下格式返回时间:11:0014:00

有人可以帮助我吗?我真的很感激一些帮助。

1 个答案:

答案 0 :(得分:2)

基本上,您希望将unix时间戳转换为为iCal指定的格式。

php manual regarding the date function中有一个很棒的小评论,其中包括这个小宝石,正是这样:

// Converts a unix timestamp to iCal format (UTC) - if no timezone is
// specified then it presumes the uStamp is already in UTC format.
// tzone must be in decimal such as 1hr 45mins would be 1.75, behind
// times should be represented as negative decimals 10hours behind
// would be -10

function unixToiCal($uStamp = 0, $tzone = 0.0) {

    $uStampUTC = $uStamp + ($tzone * 3600);       
    $stamp  = date("Ymd\THis\Z", $uStampUTC);

    return $stamp;       

} 

也许这有帮助。

相关问题