How to format date-time for RFC2445 in PHP

1.4k Views Asked by At

I am working on Google Calendar API using its PHP library and I need to set a recurring event for which I have to create a RRule string which should be of following format:

RRULE:FREQ=WEEKLY;UNTIL=20110701T170000Z

I am unable to create the date in above format. I literally tried all methods like:

date('YmdHis');
date(DATE_RFC2822);
date('c');

But Google doesn't accept any of above formats. I need to make it like: 20110701T170000Z. Can anybody help me on this?

Thanks

3

There are 3 best solutions below

0
Mel_T On BEST ANSWER

This is your date format:

date('Ymd\THis\Z')

becomes

20150429T154315Z

Look at the examples here: http://php.net/manual/de/function.date.php

0
Ye. On

Are you able to concatenate two strings together and form the required datetime string in RFC2822?

<?php
    $ymd = date('Ymd');
    $hms = date('His');
    echo $ymd."T".$hms."Z"; //Will output YYYYMMDDTHHMMSSZ
?>

Let me know if this works! :)

0
gbestard On

Try this:

date('Ymd\THis\Z', $timestamp);

or

date('Ymd\THis\Z');

if you want the current timestamp