php: writing a .ics (iCal) file? Date-formatting?

杀马特。学长 韩版系。学妹 提交于 2020-03-03 09:25:28

问题


I'm not much of a php expert and have troubles formatting the date for a .ics file.

So I have a loop that generates a calendar entry for each $post (where a $post is an event in my case)

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;

I have just problems with formatting the date that's why I got completey rid of it and hope you guys might help me.

I have two functions get_event_date($post) and get_event_end_date($post) that return a timestamp like 1258665163. How can I convert this timestamp into the right format for the iCal .ics file?

Moreover I'd also like to add the time of each event. Those two function are called get_event_time($post) and get_event_end_time($post) which both return a time in the following format: 11:00 or 14:00

Can somebody help me out here? I'd really appreciate some help.


回答1:


Basically you want to convert a unix timestamp to the format specified for iCal.

There's a wonderful little comment in the php manual regarding the date function which includes this little jewel, doing exactly that:

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

} 

Maybe this helps.



来源:https://stackoverflow.com/questions/12234925/php-writing-a-ics-ical-file-date-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!